从嵌套的 jsonArray 中提取值时面临问题

Facing issue in extracting value from nested jsonArray

我知道有人发布了类似的问题,但是答案对我不起作用。

我是这个 RestAssured.I 的新手,如果类别等于 'FUNGI' 并且特征->特征包含 'VRA',我想获得 'uuid' 值。这是我的样本json:

[{
      "uuid":"e223d29b-499b-b58b-995e-654bef1aab03",
      "categories":[
         {
            "uuid":"89d1c022-4453-5f6b-883c-46730d429b2a",
            "name":"FERTI"
         }
      ],
      "minRateSi":0.0
   },
   {
      "uuid":"93015a1b-76ac-2ca3-2bbc-5ae5480962c2",
      "categories":[
         {
            "uuid":"61c951b1-3e47-f0a0-80d8-3d43efa339fb",
            "name":"FUNGI"
         }
      ],
      "minRateSi":0.0,
      "maxRateSi":7.5E-8,
      "features":[
         {
            "id":"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f",
            "vraMinRate":2.0E-8,
            "features":[
               "VRA"
            ]
         },
         {
            "id":"ec0d0f52-dd71-ebb9-0a39-831768fe4490",
            "vraMinRateSi":3.0E-8,
            "features":[
               "VRA"
            ]
         }
      ]
   },
   {
      "uuid":"38290452-4937-4f33-c54d-7f502b84ed99",
      "categories":[
         {
            "uuid":"2c9d8cc0-01bc-899d-6782-cf412e90fd78",
            "name":"FUNGI"
         }
      ],
      "maxRateSi":1.0E-7,
      "features":[
         {
            "id":"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f",
            "vraMinRateSi":6.5E-8
         },
         {
            "cropUuid":"ec0d0f52-dd71-ebb9-0a39-831768fe4490",
            "vraMinRateSi":5.0E-8
         }
      ]}]

Woo,好吧,你的案子很难解决,但只要你知道如何使用 JSONArrayJSONObject 类,你就会没事的。

首先让我提一下你应该如何处理这种情况。


我更喜欢使用在线 JSON 格式化程序 like this one 只需粘贴您的 JSON 负载,您就可以判断是否使用JSONObjectJSONArray.

请在此处备注,并查看how to parse json payloads

这段代码对你有用,尽管你需要找出它背后的逻辑并实现JSON解析相应地在你以后的情况下。

//declaring your payload
String myJsonPayload = "[{\r\n" + "      \"uuid\":\"e223d29b-499b-b58b-995e-654bef1aab03\",\r\n" +
 "      \"categories\":[\r\n" + "         {\r\n" +
 "            \"uuid\":\"89d1c022-4453-5f6b-883c-46730d429b2a\",\r\n" +
 "            \"name\":\"FERTI\"\r\n" + "         }\r\n" + "      ],\r\n" +
 "      \"minRateSi\":0.0\r\n" + "   },\r\n" + "   {\r\n" +
 "      \"uuid\":\"93015a1b-76ac-2ca3-2bbc-5ae5480962c2\",\r\n" + "      \"categories\":[\r\n" +
 "         {\r\n" + "            \"uuid\":\"61c951b1-3e47-f0a0-80d8-3d43efa339fb\",\r\n" +
 "            \"name\":\"FUNGI\"\r\n" + "         }\r\n" + "      ],\r\n" +
 "      \"minRateSi\":0.0,\r\n" + "      \"maxRateSi\":7.5E-8,\r\n" + "      \"features\":[\r\n" +
 "         {\r\n" + "            \"id\":\"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f\",\r\n" +
 "            \"vraMinRate\":2.0E-8,\r\n" + "            \"features\":[\r\n" +
 "               \"VRA\"\r\n" + "            ]\r\n" + "         },\r\n" + "         {\r\n" +
 "            \"id\":\"ec0d0f52-dd71-ebb9-0a39-831768fe4490\",\r\n" +
 "            \"vraMinRateSi\":3.0E-8,\r\n" + "            \"features\":[\r\n" +
 "               \"VRA\"\r\n" + "            ]\r\n" + "         }\r\n" + "      ]\r\n" + "   },\r\n" +
 "   {\r\n" + "      \"uuid\":\"38290452-4937-4f33-c54d-7f502b84ed99\",\r\n" +
 "      \"categories\":[\r\n" + "         {\r\n" +
 "            \"uuid\":\"2c9d8cc0-01bc-899d-6782-cf412e90fd78\",\r\n" +
 "            \"name\":\"FUNGI\"\r\n" + "         }\r\n" + "      ],\r\n" +
 "      \"maxRateSi\":1.0E-7,\r\n" + "      \"features\":[\r\n" + "         {\r\n" +
 "            \"id\":\"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f\",\r\n" +
 "            \"vraMinRateSi\":6.5E-8\r\n" + "         },\r\n" + "         {\r\n" +
 "            \"cropUuid\":\"ec0d0f52-dd71-ebb9-0a39-831768fe4490\",\r\n" +
 "            \"vraMinRateSi\":5.0E-8\r\n" + "         }\r\n" + "      ]}]";

JSONArray json = new JSONArray(myJsonPayload);
System.out.println(json);

for (int i = 0; i < json.length(); i++) {
 JSONObject object = (JSONObject) json.get(i);
 // System.out.println(object); // you have each JSONObject { } contained in the
 // Outer JSONArray [ ]
 //Getting both uuid's since you didn't answer which one.
 String uuid = object.getString("uuid");
 System.out.println("\n\n Next Object - Outer UUID: " + uuid);
 JSONArray categories = object.getJSONArray("categories");
 System.out.println(categories);
 if (((JSONObject) categories.get(0)).getString("name").equalsIgnoreCase("fungi")) {
  System.out.println("\nFUNGI found!:\n");
  //Getting the inner UUID
  String uuidOfFungi = ((JSONObject) categories.get(0)).getString("uuid");
  System.out.println("Inner UUID: " + uuidOfFungi);
 } else {
  System.out.println("\nFUNGI NOT FOUND (ABORTING):\n");
 }


 // Getting Features:
 try {
  JSONArray featuresObject = object.getJSONArray("features");

  for (int index = 0; index < featuresObject.length(); index++) {
   try {
    JSONObject features = featuresObject.getJSONObject(index);
    //Getting VRA
    String vraFeatues = features.getJSONArray("features").getString(0);

   } catch (JSONException e) {
    System.out.println("No inner JSONArray in features JSONObject");
   }
  }


 } catch (JSONException e) {
  System.out.println("JSON OBJECT " + i + " HAS NO FEATURES ARRAY");
 }


}


再次取决于您的用例,如何将 JSON 有效载荷放入您的程序中取决于您。

  • Getting it from remote URL using Java
  • Passing it as an argument on java -jar execution time ,我真的不推荐

无论哪种情况,都需要进行修改以相应地初始化 myJsonPayload

希望对您有所帮助!