2016-12-02 253 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,價值..是列名和我們可以有更多的L的以及(它是動態的)
我希望它轉換成以下形式

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

欲優選JSONArray或列表結構的結果。 有沒有人有算法或代碼來做到這一點在Java? 謝謝

+0

什麼是L0,L1 ...?你可以等到L3還是可以有更多? – Roxy

+0

在問題中更新了它。謝謝。 – AngryLeo

回答

3

遞歸是你的朋友。

下面的代碼根據輸入數據構建了一箇中間模型(「節點」樹)的層次結構。

這便化作使用遞歸方法JSON ...

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; 
     } 
    } 
} 

輸出:

[ { 
    "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會被刪除。由於這個輸出成爲: L0標籤爲ID1等...請幫助! –

+0

結果我所要做的只是交換獲取數據的列 更新的格式是: 'L0 | id0 |' –