首页 > 解决方案 > 如何使用数组列表解析 json 响应

问题描述

我有一个以下响应,由于响应从对象 0 开始,我无法验证。JSON这意味着如果有多个对象,响应将从 0 开始到您拥有的对象数。

我已经尝试过了,但它不起作用并最终出现堆栈溢出错误。

public static void Location_ContactValidation(Response response, String code, String message, ExtentTest log) {
 try {
  softAssert = new SoftAssert();
  org.json.JSONObject jsonObject = new org.json.JSONObject(response);
  org.json.JSONObject getSth = jsonObject.getJSONObject("status");
  status_Message = getSth.get("message");
  softAssert.assertEquals(code, code);
  softAssert.assertEquals(message, status_Message);
  log.log(LogStatus.INFO, "Validation: The status code is " + code);
  log.log(LogStatus.INFO, "Validation: The status message is " + status_Message.toString());

 } catch (Exception e) {
  log.log(LogStatus.INFO, "Validation: The status code is " + code);
  if (status_Message != null) {
   log.log(LogStatus.INFO, "Validation: The status message is " + status_Message.toString());
  }
  System.out.println(e.getMessage());
  if (softAssert != null) {
   softAssert.assertAll();
  }
 }
}

堆栈溢出错误作为流 -

java.lang.StackOverflowError
    at org.json.JSONObject.wrap(JSONObject.java:1746)
    at org.json.JSONArray.<init>(JSONArray.java:176)
    at org.json.JSONObject.wrap(JSONObject.java:1747)
    at org.json.JSONObject.populateMap(JSONObject.java:1167)

这是我要解析的响应

{
    "0": {
        "status": "OK",
        "data": {
            "id": "*************",
            "mobile": "*************"
        },
        "message": "Submitted Successfully"
    },
    "status": "OK"
}

我需要验证手机号码,包括状态和消息。但做不到。如果请求再发送一个数字,则响应会增加,并且首先创建数组,如使用0then with所示1

我感谢您的帮助。

标签: javaarraysjson

解决方案


JSONObject您可以列出给定usingkeySet方法的所有键。之后,您可以直接挖掘必填字段。

下面的代码显示了如何读取所有必填字段:

import org.json.JSONObject;

import java.io.File;
import java.nio.file.Files;

public class OrgJsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();
        String json = String.join("", Files.readAllLines(jsonFile.toPath()));

        JSONObject response = new JSONObject(json);
        response.keySet().forEach(key -> {
            JSONObject object = response.optJSONObject(key);
            // if given key represents object it must be data
            if (object != null) {
                final String dataKey = "data";
                JSONObject data = object.optJSONObject(dataKey);
                // extract mobile from data and remove data
                // this way JSON node is much simpler
                if (data != null) {
                    final String mobileKey = "mobile";
                    String mobile = data.optString(mobileKey);
                    System.out.println("Mobile => " + mobile);
                }
                System.out.println("status => " + object.optString("status"));
                System.out.println("message => " + object.optString("message"));
            }
        });
    }
}

对于以下JSON有效载荷:

{
  "0": {
    "status": "OK",
    "data": {
      "id": "1",
      "mobile": "44-32-12"
    },
    "message": "Submitted Successfully"
  },
  "1": {
    "status": "OK",
    "data": {
      "id": "2",
      "mobile": "9981234-543"
    },
    "message": "Submitted Successfully"
  },
  "status": "OK"
}

印刷:

Mobile => 44-32-12
status => OK
message => Submitted Successfully
Mobile => 9981234-543
status => OK
message => Submitted Successfully

推荐阅读