首页 > 解决方案 > 有多个 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
  }
}

标签: javajsongsonjsonparser

解决方案


发布的 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);

推荐阅读