gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:05:12 24 4
gpt4 key购买 nike

我有一个以下响应,由于响应从 JSON 对象 0 开始,所以我无法验证该响应。这意味着如果有多个对象,则响应将从 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"
}

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

感谢您的帮助。

最佳答案

您可以使用 keySet 方法列出给定 JSONObject 的所有键。之后您可以直接挖掘所需字段。

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

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

关于java - 如何使用数组列表解析 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55381774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com