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

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

最佳答案

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

关于java - 使用 GSON 和多个内部对象解析 JSON。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23758252/

相关文章:

api - 如果我发布我的开源项目的后端代码,是否存在安全问题?

android - 关于如何在 android 的安装类中使用 Rest API(服务)传递 ParseObject(对象)?

java - Eclipse maven 仅管理依赖项,仅此而已

java - 按整数排序列表

java - Eclipse 和 BIRT 错误 - 在 Web 查看器中查看报告

c# - 将 json 数组转换为数据表 asp.net

java - 无法使用本地 Tomcat 服务器上的 Java REST-API 连接到 pgsql 数据库。堆栈包含 ExceptionInInitializerError 和 IllegalStateException

Java库兼容性问题-NULL指针异常

php - 从mysql输出json指定数据

javascript - select2 4.0 AJAX 预填充方法?