如何使用Java遍历嵌套的JSON数据

时间:2019-02-19 14:45:33

标签: java arrays json

我正在尝试从嵌套的JSON数据文件中获取AlphaBetaGamma的值。

我正在尝试使用类似这样的方法来获取Alpha

Object o = new JSONParser().parse(new FileReader(newFile("C:\\Users\\abc\\Documents\\file.json")));
JSONObject jo = (JSONObject) o;

JSONArray resultCode = (JSONArray) jo.get("mkDetails");

JSONObject x = (JSONObject) resultCode.get(0);
x.get("Alpha").toString()       

这是JSON:

{
  "metadata": {
    "offset": 0,
    "psize": 10
  },
  "svc": [
    {
      "mNumber": "225",
      "markIp": {
        "Name": "Ant",
        "eDate": "3006-08-01",
        "cDate": "9999-12-31"
      },
      "mkDetails": [
        {
          "Alpha": "D",
          "Beta": "S"
        }
      ],
      "mNetDetails": [
        {
          "Gaama": "213",
          "mkTypeCode": "23"
        }
      ]
    }
  ],
  "serviceFault": {
    "faultType": null,
    "faultCode": null,
    "message": null
  }
}

2 个答案:

答案 0 :(得分:0)

您不能只做jo.get("mkDetails"),因为jo没有密钥mkDetails。您必须自己遍历树的每个级别:

JSONObject root = ...;

JSONArray svc = (JSONArray) root.get("svc");
JSONObject svc0 = (JSONObject) svc.get(0);
JSONObject mkDetails = (JSONObject) svc0.get("mkDetails");
JSONObject mNetDetails = (JSONObject) svc0.get("mNetDetails");

String alpha = (String) mkDetails.get("Alpha");
String beta = (String) mkDetails.get("Beta");
String gamma = (String) mNetDetails.get("Gamma");

答案 1 :(得分:0)

这是一个可能的解决方案。我正在使用Google的Gson lib。您可以通过maven导入。

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

这是解析JSON的代码。

    import com.google.gson.Gson;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;

    public static void parseJson (){

     String thisJson ="{\r\n" + 
            "  \"metadata\": {\r\n" + 
            "    \"offset\": 0,\r\n" + 
            "    \"psize\": 10\r\n" + 
            "  },\r\n" + 
            "  \"svc\": [\r\n" + 
            "    {\r\n" + 
            "      \"mNumber\": \"225\",\r\n" + 
            "      \"markIp\": {\r\n" + 
            "        \"Name\": \"Ant\",\r\n" + 
            "        \"eDate\": \"3006-08-01\",\r\n" + 
            "        \"cDate\": \"9999-12-31\"\r\n" + 
            "      },\r\n" + 
            "      \"mkDetails\": [\r\n" + 
            "        {\r\n" + 
            "          \"Alpha\": \"D\",\r\n" + 
            "          \"Beta\": \"S\"\r\n" + 
            "        }\r\n" + 
            "      ],\r\n" + 
            "      \"mNetDetails\": [\r\n" + 
            "        {\r\n" + 
            "          \"Gaama\": \"213\",\r\n" + 
            "          \"mkTypeCode\": \"23\"\r\n" + 
            "        }\r\n" + 
            "      ]\r\n" + 
            "    }\r\n" + 
            "  ],\r\n" + 
            "  \"serviceFault\": {\r\n" + 
            "    \"faultType\": null,\r\n" + 
            "    \"faultCode\": null,\r\n" + 
            "    \"message\": null\r\n" + 
            "  }\r\n" + 
            "}";
     Gson gson = new Gson();

     JsonElement jelement = new JsonParser().parse(thisJson);
     JsonObject  jobject = jelement.getAsJsonObject();
     JsonArray jarray = jobject.getAsJsonArray("svc");
     jobject = jarray.get(0).getAsJsonObject();

     JsonArray netDetailsArr = jobject.getAsJsonArray("mNetDetails");
     JsonArray mkDetailsArr =  jobject.getAsJsonArray("mkDetails");

     jobject = netDetailsArr.get(0).getAsJsonObject();
     String gamma = jobject.get("Gaama").getAsString();
     System.out.println("gamma==>" + gamma);

     jobject = mkDetailsArr.get(0).getAsJsonObject();
     String alpha = jobject.get("Alpha").getAsString();
     String beta = jobject.get("Beta").getAsString();

     System.out.println("alpha ==>" + alpha);
     System.out.println("beta ===>"+ beta);

 }

控制台输出:

gamma==>213
alpha ==>D
beta ===>S