简体   繁体   English

使用GSON和多个内部对象来解析JSON。

[英]Parsing JSON using GSON with multiple inner objects.

I would like to parse the following JSON response, using GSON. 我想使用GSON解析以下JSON响应。 As you can see, there are a lot of inner json objects in the response. 如您所见,响应中有很多内部json对象。 Do I need to create POJO classes for all the inner objects or is there any alternative? 我是否需要为所有内部对象创建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"
            }
        }
    ]
}

Simply convert the JSON string into Map. 只需将JSON字符串转换为Map。

Sample code: 样例代码:

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();

You can do it using some POJO classes as well which are replica of the JSON string that is more simple and shorter. 您也可以使用一些POJO类来实现它,它们是JSON字符串的副本,更简单,更短。

Sample code: 样例代码:

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());

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM