简体   繁体   English

如何使用数组列表解析json响应

[英]How to parse json response with array list

I have a below response which I am unable to validate due to response is starting from JSON object 0. Thats means if more than one object, the response will start from 0 to the number of objects you have. 我有一个下面的响应,由于响应是从JSON对象0开始的,所以我无法验证。这意味着如果一个以上的对象,响应将从0开始到您拥有的对象数。

I have tried this but its not working and ends up with stack over flow error. 我已经尝试过了,但是它不起作用,最终导致堆栈溢出错误。

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();
  }
 }
}

Stack overflow error as flows- 流的堆栈溢出错误-

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)

And here is the response I want to parse 这是我想解析的回复

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

I need to validate the mobile number, both the status and message. 我需要验证手机号码,包括状态和消息。 But not able to do it. 但是无法做到。 If one more number is send with request then the response increases and to array gets created first as shown with 0 then with 1 . 如果通过请求再发送一个数字,则响应会增加,首先创建数组,如0所示,然后是1

I appreciate your help. 我感谢您的帮助。

You can list all keys for given JSONObject using keySet method. 您可以使用keySet方法列出给定JSONObject所有键。 After that you can directly dig into required fields. 之后,您可以直接挖掘必填字段。

Below codes shows how to read all required fields: 以下代码显示了如何读取所有必填字段:

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"));
            }
        });
    }
}

For below JSON payload: 对于以下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"
}

prints: 打印:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM