2014-05-20 2 views
0

GSON을 사용하여 다음 JSON 응답을 구문 분석하고 싶습니다. 보시다시피 응답에는 내부 json 객체가 많이 있습니다. 모든 내부 객체에 대해 POJO 클래스를 생성해야합니까 아니면 다른 대안이 있습니까?다중 내부 개체가있는 GSON을 사용하여 JSON을 구문 분석합니다.

{ 
    "nodeProperties": [ 
     { 
      "properties": { 
       "timeStamp": { 
        "value": 1400475483694, 
        "name": "connectedSince" 
       }, 
       "macAddress": { 
        "value": "00:00:00:00:00:03" 
       }, 
       "tables": { 
        "value": -1 
       }, 
       "capabilities": { 
        "value": 199 
       }, 
       "tier": { 
        "value": 1 
       }, 
       "supportedFlowActions": { 
        "value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]" 
       }, 
       "buffers": { 
        "value": 256 
       }, 
       "description": { 
        "value": "NEXT_NEWSwitch3" 
       }, 
       "forwarding": { 
        "value": 0 
       } 
      }, 
      "node": { 
       "id": "00:00:00:00:00:00:00:03", 
       "type": "OF" 
      } 
     }, 
     { 
      "properties": { 
       "timeStamp": { 
        "value": 1400475481261, 
        "name": "connectedSince" 
       }, 
       "macAddress": { 
        "value": "00:00:00:00:00:02" 
       }, 
       "tables": { 
        "value": -1 
       }, 
       "capabilities": { 
        "value": 199 
       }, 
       "tier": { 
        "value": 1 
       }, 
       "supportedFlowActions": { 
        "value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]" 
       }, 
       "buffers": { 
        "value": 256 
       }, 
       "description": { 
        "value": "None" 
       }, 
       "forwarding": { 
        "value": 0 
       } 
      }, 
      "node": { 
       "id": "00:00:00:00:00:00:00:02", 
       "type": "OF" 
      } 
     }, 
     { 
      "properties": { 
       "timeStamp": { 
        "value": 1400475478695, 
        "name": "connectedSince" 
       }, 
       "macAddress": { 
        "value": "00:00:00:00:00:01" 
       }, 
       "tables": { 
        "value": -1 
       }, 
       "capabilities": { 
        "value": 199 
       }, 
       "supportedFlowActions": { 
        "value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]" 
       }, 
       "buffers": { 
        "value": 256 
       }, 
       "description": { 
        "value": "None" 
       }, 
       "forwarding": { 
        "value": 0 
       } 
      }, 
      "node": { 
       "id": "00:00:00:00:00:00:00:01", 
       "type": "OF" 
      } 
     } 
    ] 
} 
+2

은'gson' 라이브러리는 쉽고 간단한 자바 ** ** 클래스'JSON'의 개체를 변환 할 수 있습니다. 이러한 객체를 클래스로 나타내려면 ** yes ** POJO 클래스를 만들어야합니다. 반드시'JSON'을 ** Map ** 등으로 변환하기를 원한다면'gson'을 사용하지 말아야합니다. –

+0

JSON을 만든 사람은 누구나 너무 밝지는 않지만 구문 분석하는 것은 간단합니다 JSON 구조를 정확하게 반영하는지도 및 목록으로 변환합니다. 하지만 GSON과 Jackson은 일반적으로 좋은 선택이 아닙니다. –

+0

@HotLicks JSON 구조가 좋지 않다고 생각하는 이유를 설명해 주시겠습니까? – vais

답변

1

JSON 문자열을 Map으로 변환하기 만하면됩니다.

샘플 코드 :

FileReader in = new FileReader(new File("resources/json3.txt")); 
BufferedReader br = new BufferedReader(in); 

// Convert JSON string into MAP object 
Gson gson = new Gson(); 
Type type = new TypeToken<Map<String, ArrayList<Map<String, Map<String, Object>>>>>() {}.getType(); 
Map<String, ArrayList<Map<String, Map<String, Object>>>> map = gson.fromJson(br, type); 

for (String key : map.keySet()) { 
    System.out.println(key); 
    for (Map<String, Map<String, Object>> value : map.get(key)) { 
     for (String k : value.keySet()) { 
      System.out.println(k); 
      for (String k1 : value.get(k).keySet()) { 
       System.out.println(k1 + ":" + value.get(k).get(k1)); 
      } 
     } 
     System.out.println("--------------"); 
    } 
} 

in.close(); 
br.close(); 

좀 더 간단하고 짧은있는 JSON 문자열의 복제입니다뿐만 아니라 일부 POJO 클래스를 사용하여 그것을 할 수 있습니다.

샘플 코드 :

class PropertiesNode { 
    Properties properties; 
    Node node; 
    // getter & setter 
} 

class Node { 
    String id; 
    String type; 
    // getter & setter 
} 

class Properties { 
    Map<String, Object> timeStamp; 
    Map<String, String> macAddress; 
    Map<String, Integer> tables; 
    Map<String, Integer> capabilities; 
    Map<String, Integer> tier; 
    Map<String, String> supportedFlowActions; 
    Map<String, Integer> buffers; 
    Map<String, String> description; 
    Map<String, Integer> forwarding; 
    // getter & setter 
} 

Gson gson = new Gson(); 
Type type = new TypeToken<Map<String, ArrayList<PropertiesNode>>>() {}.getType(); 
Map<String, ArrayList<PropertiesNode>> nodeProperties = gson.fromJson(br, type); 
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(nodeProperties).toString()); 

출력 :

{ 
    "nodeProperties": [ 
     { 
     "properties": { 
      "timeStamp": { 
      "value": 1.400475483694E12, 
      "name": "connectedSince" 
      }, 
      "macAddress": { 
      "value": "00:00:00:00:00:03" 
      }, 
      "tables": { 
      "value": -1 
      }, 
      "capabilities": { 
      "value": 199 
      }, 
      "tier": { 
      "value": 1 
      }, 
      "supportedFlowActions": { 
      "value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]" 
      }, 
      "buffers": { 
      "value": 256 
      }, 
      "description": { 
      "value": "NEXT_NEWSwitch3" 
      }, 
      "forwarding": { 
      "value": 0 
      } 
     }, 
     "node": { 
      "id": "00:00:00:00:00:00:00:03", 
      "type": "OF" 
     } 
     }, 
     { 
     "properties": { 
      "timeStamp": { 
      "value": 1.400475481261E12, 
      "name": "connectedSince" 
      }, 
      "macAddress": { 
      "value": "00:00:00:00:00:02" 
      }, 
      "tables": { 
      "value": -1 
      }, 
      "capabilities": { 
      "value": 199 
      }, 
      "tier": { 
      "value": 1 
      }, 
      "supportedFlowActions": { 
      "value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]" 
      }, 
      "buffers": { 
      "value": 256 
      }, 
      "description": { 
      "value": "None" 
      }, 
      "forwarding": { 
      "value": 0 
      } 
     }, 
     "node": { 
      "id": "00:00:00:00:00:00:00:02", 
      "type": "OF" 
     } 
     }, 
     { 
     "properties": { 
      "timeStamp": { 
      "value": 1.400475478695E12, 
      "name": "connectedSince" 
      }, 
      "macAddress": { 
      "value": "00:00:00:00:00:01" 
      }, 
      "tables": { 
      "value": -1 
      }, 
      "capabilities": { 
      "value": 199 
      }, 
      "supportedFlowActions": { 
      "value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]" 
      }, 
      "buffers": { 
      "value": 256 
      }, 
      "description": { 
      "value": "None" 
      }, 
      "forwarding": { 
      "value": 0 
      } 
     }, 
     "node": { 
      "id": "00:00:00:00:00:00:00:01", 
      "type": "OF" 
     } 
     } 
    ] 
    } 
+0

감사합니다. 이 방법이 효과적이긴하지만지도 대신 클래스 속성에 멤버 변수 POJO를 만들도록 선택했습니다. – vais