java - 如何使用数组列表解析 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"
}
我需要验证手机号码,包括状态和消息。但做不到。如果请求再发送一个数字,则响应会增加,并且首先创建数组,如使用0
then with所示1
。
我感谢您的帮助。
解决方案
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
推荐阅读
- java - 滚动窗格垂直滚动条不显示滚动旋钮
- python - 如何创建单独的文件以生成不同的数据库 Flask-SqlAlchemy 表?
- reactjs - react-virtualized ,在哪里调用 forceUpdateGrid 来更新表格数据?
- arrays - 如何在树枝的第二层使用动态键/值?
- database - Firebase 功能未执行
- node.js - Web 和移动设备,存储令牌/cookie 的位置
- php - Codeingiter 构造函数无法正常工作
- javascript - 我的函数没有返回一个数组,即使它真的应该
- lumen - Lumen 5.7(Laravel)中请求的包'composer require nesbot/carbon'问题
- c# - 在用户友好的消息中包装异常