java - 有多个 ID 值时解析 JSONObject 以访问 ID(Java)
问题描述
我正在从一个 API 获取 JSON 响应,该响应为我提供了格式为 JSON 的呼叫记录列表。我想解析数据并找到记录ID,我的麻烦是每条JSON记录都有多个ID,我不确定如何访问正确的ID。请记住,在运行请求之前,我不知道 ID 的值是“3461487000073355176”。
这是我接收 JSON 的代码,我创建了一个 JSONObject,因此我希望可以存储该值。
1.
Response response = client.newCall(request).execute();
String responseBody = response.body().string();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser parser = new JsonParser();
JsonElement je = parser.parse(responseBody);
String prettyJsonString = gson.toJson(je);
JSONObject json = new JSONObject(prettyJsonString);
System.out.println("Json = " + json);
我需要访问的 ID 的 JSON 旁边有一条注释:
"data": [
{
"Owner": {
"name": "My namen",
"id": "346148700000017",
"email": "m@gmail.com"
},
"$state": "save",
"$process_flow": false,
"Street": "95## ### ######",
"id": "**3461487000073355176**", ----This is the ID I need -----
"Coverage_A_Dwelling": 100000,
"$approval": {
"delegate": false,
"approve": false,
"reject": false,
"resubmit": false
},
"Created_Time": "2020-12-10T09:05:17-05:00",
"Property_Details": "Primary Residence",
"Created_By": {
"name": "My name",
"id": "346148700000017",
"email": "m@gmail.com"
},
"Description": "Created on Jangl: https://jan.gl/crwp773ytg8",
"$review_process": {
"approve": false,
"reject": false,
"resubmit": false
},
"Property_State": "FL",
"Property_Street": "95",
"Roof_Material": "Asphalt Shingle",
"Full_Name": "Clare Em",
"Property_City": "Land ",
"Email_Opt_Out": false,
"Lead_I_D": "4FFEC0C5-FBA1-2463-DB9B-C38",
"Insured_1_DOB": "1942-02-20",
"$orchestration": false,
"Tag": [],
"Email": "cr@yahoo.com",
"$currency_symbol": "$",
"$converted": false,
"Zip_Code": "338",
"$approved": true,
"$editable": true,
"City": "Land O Lakes",
"State": "FL",
"Structure_Type": "Single Family",
"Prior_Carrier": {
"name": "Default Carrier (DO NOT DELETE OR CHANGE)",
"id": "3461487000000235093"
},
"Source": {
"name": "EverQ",
"id": "346148700006474"
},
"First_Name": "Clarence",
"Modified_By": {
"name": "My name",
"id": "3461487000000172021",
"email": "m@gmail.com"
},
"Phone": "7036159075",
"Modified_Time": "2020-12-10T09:05:17-05:00",
"$converted_detail": {},
"Last_Name": "####",
"$in_merge": false,
"$approval_state": "approved",
"Property_Zip": "34638"
}
],
"info": {
"per_page": 200,
"count": 1,
"page": 1,
"more_records": false
}
}
解决方案
发布的 JSON 响应缺少初始“{”。
您的 JSON 包含data
,这是一个JSONArray
对象Owner
。获取id
第一个所有者的字段(数组元素 0):
// existing code
JSONObject json = new JSONObject(prettyJsonString);
System.out.println("Json = " + json);
// get the id field
JSONArray dataArray = (JSONArray)json.get("data");
JSONObject data0 = (JSONObject) dataArray.get(0);
JSONObject owner = (JSONObject) data0.get("Owner");
String id = owner.getString("id");
System.out.println(id);
推荐阅读
- firebase - flutter web) 如何设置firebase auth Persistence
- database - Apache IoTDB中查询的一般流程是什么?
- windows - 如何使用 Perl 中的 Win32::LongPath 模块来操作长路径名?
- css - 每行/字符的颜色渐变
- java - 使用 Selenium 提取父元素的属性值
- c++ - 如何使用 Unreal 在 C++ 中解析 JSON 文件的数组
- ruby - 在活动管理表单中看不到 Quill 编辑器
- flutter - 当 itemCount 是动态的时,颤动奇怪的随机滑动
- ruby-on-rails - 路由到一个特殊的控制器动作
- c# - 如何在计算器程序中添加第二个参数(数字)?