2016-12-02 16 views
-1

私は、次のような構造であるフラットなデータの形式でSQL結果セットに変更フラットコラム構造データの階層構造に

L0|L1|L2|L3|value 

n1|o1|p1|c1|3 
n1|o1|p1|c2|2 
n1|o1|p2|c1|1 
n1|o2|p1|c1|0 
n2|o2|p1|c1|5 

を取得しています。ここL0、L1、L2、バリュー...は列の名前であり、私たちは、私はそれが私が好ましくJSONArrayまたはリスト構造の結果をしたい以下のフォーム

[{name:"n1",children: 
    [{name:o1,children: 
     [{name:"p1",children: 
      [{name:"c1",value:3}, 
      {name:"c2",value:2}] 
     },{name:"p2",children: 
      [{name:"c1",value:"1"}] 
     }],]}..... 

に変換する(それが動的である)
だけでなく、よりL'sのを持つことができます。 誰でもJavaでそれを行うためのアルゴリズムやコードを持っていますか? ありがとう

+0

L0、L1は...何ですか?あなたはL3まで持っていることができますか、それ以上持っている可能性はありますか? – Roxy

+0

は質問で更新しました。ありがとうございます。 – AngryLeo

答えて

3

再帰はあなたの友人です。

以下のコードは、入力データから中間モデル(「ノード」のツリー)に階層を構築します。

これは、その後、

import java.util.HashMap; 
import java.util.Map; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class Stack { 

    public static void main(String[] args) { 
     // Can have as many columns as you want, always assume the last column is the value. 
     Object[][] dataFromResultSet = { 
       { "n1", "o1", "p1", "c1", 3 },  
       { "n1", "o1", "p1", "c2", 2 },  
       { "n1", "o1", "p2", "c1", 1 },  
       { "n1", "o2", "p1", "c1", 0 },  
       { "n2", "o2", "p1", "c1", 5 }  
     }; 

     Node root = new Node(); 

     // Add all records to the hierachy 
     for (Object[] row : dataFromResultSet) { 
      addToHierachy(root, row); 
     } 

     // Convert hierachy to JSON 
     try { 
      JSONArray json = convertToJSON(root); 
      System.out.println(json); 
     } catch (JSONException e) { 
      System.out.println("Something went wrong converting hierachy to JSON"); 
      e.printStackTrace(); 
     } 
    } 

    private static void addToHierachy(Node root, Object[] row) { 
     Node current = root; 

     // Go through each column in the row 
     for(Object col : row) { 
      // If this column is a string, then it is a Branch node, not a value one 
      // (It might be better to iterate through the array using a counter instead 
      // and change this condition to say "if it isn't the last column"...) 
      if(col instanceof String) { 
       // Get (or create) the child node for this column 
       current = current.getOrCreateChild((String) col); 
      } else { 
       // Otherwise, set the value 
       current.setValue((Integer) col); 
      } 
     } 
    } 

    private static JSONArray convertToJSON(Node root) throws JSONException { 
     // Use recursion to build the result JSON 
     JSONArray array = new JSONArray(); 

     // Starting at this root, go through all of the child entries 
     for(Map.Entry<String, Node> child : root.getChildren().entrySet()) { 
      Node childNode = child.getValue(); 

      // New object for this entry... 
      JSONObject object = new JSONObject(); 
      // Set the name 
      object.put("name", child.getKey()); 

      // Set the value if it is present on this node 
      if(childNode.getValue() != null) { 
       object.put("value", childNode.getValue()); 
      } 

      // Generate the child hierarchy if it has children 
      if(!childNode.getChildren().isEmpty()) { 
       JSONArray childHierachy = convertToJSON(childNode); 
       object.put("children", childHierachy); 
      } 

      array.put(object); 
     } 

     return array; 
    } 

    // Class used to build the hierarchy 
    static class Node { 
     // The map of children, LABEL -> NODE 
     private Map<String, Node> children = new HashMap<>(); 
     // The value (kept as null if this does not have a value set) 
     private Integer value; 

     public Node getOrCreateChild(String key) { 
      Node node = children.get(key); 

      if(node == null) { 
       node = new Node(); 
       children.put(key, node); 
      } 

      return node; 
     } 

     public Map<String, Node> getChildren() { 
      return children; 
     } 

     public Integer getValue() { 
      return value; 
     } 

     public void setValue(int value) { 
      this.value = value; 
     } 
    } 
} 

出力...再帰的な方法を使用してJSONになっている:

[ { 
    "name" : "n1", 
    "children" : [ { 
     "name" : "o2", 
     "children" : [ { 
      "name" : "p1", 
      "children" : [ { 
       "name" : "c1", 
       "value" : 0 
      } ] 
     } ] 
    }, { 
     "name" : "o1", 
     "children" : [ { 
      "name" : "p2", 
      "children" : [ { 
       "name" : "c1", 
       "value" : 1 
      } ] 
     }, { 
      "name" : "p1", 
      "children" : [ { 
       "name" : "c1", 
       "value" : 3 
      }, { 
       "name" : "c2", 
       "value" : 2 
      } ] 
     } ] 
    } ] 
}, { 
    "name" : "n2", 
    "children" : [ { 
     "name" : "o2", 
     "children" : [ { 
      "name" : "p1", 
      "children" : [ { 
       "name" : "c1", 
       "value" : 5 
      } ] 
     } ] 
    } ] 
} ] 
+0

ありがとうございました。 私の入力は 'id0 | L0 | id1 | L1 | id2 | L2] id3 | L3 |' であり、 'root.getChildren'を実行すると最初のIDが削除されます。このため、出力は次のようになります。 id1にタグ付けされたL0など...助けてください! –

+0

私がしなければならなかったのは、データを取得していた列を交換することでした。 新しい形式: 'L0 | id0 |' –