使用具有多个内部对象的GSON解析JSON。

时间:2014-05-20 11:14:04

标签: java json rest gson opendaylight

我想使用GSON解析以下JSON响应。如您所见,响应中有很多内部json对象。我是否需要为所有内部对象创建POJO类,还是有其他选择?

{
    "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"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分: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();

你可以使用一些POJO类来完成它,它们是更简单和更短的JSON字符串的复制品。

示例代码:

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"
        }
      }
    ]
  }