簡體   English   中英

使用GSON和多個內部對象來解析JSON。

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

我想使用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"
            }
        }
    ]
}

只需將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"
        }
      }
    ]
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM