繁体   English   中英

在Java中,如何从子数组中的值提取json数组的子集?

[英]How to extract a subset of json array from value in sub array, in java?

以下是我的json:

{
    "id": null,
    "extId": "720916740",
    "legacyId": null,
    "customerId": null,
    "accountId": null,
    "clientId": null,
    "publisherId": null,
    "name": "AllColumns1_1482141239819",
    "status": "ACTIVE",
    "operationStatus": null,
    "startDate": "20180101",
    "endDate": null,
    "channel": "SEARCH",
    "lastSyncDate": null,
    "linkStatus": "NOT_LINKED"
  },
  {
    "id": null,
    "extId": "720916743",
    "legacyId": null,
    "customerId": null,
    "accountId": null,
    "clientId": null,
    "publisherId": null,
    "name": "AllColumns2_1482141239819",
    "status": "ACTIVE",
    "operationStatus": null,
    "startDate": "20180101",
    "endDate": null,
    "channel": "SEARCH",
    "lastSyncDate": null,
    "linkStatus": "NOT_LINKED"
  },
  {
    "id": null,
    "extId": "720933833",
    "legacyId": null,
    "customerId": null,
    "accountId": null,
    "clientId": null,
    "publisherId": null,
    "name": "2TestCamp2_1482149078243",
    "status": "ACTIVE",
    "operationStatus": null,
    "startDate": "20180101",
    "endDate": null,
    "channel": "SEARCH",
    "lastSyncDate": null,
    "linkStatus": "NOT_LINKED"
  }

从上面的json,我想获取名称为“ 2TestCamp2_1482149078243”的id和extid。 我如何在JAVA中获得它?

通常,首先要从json数组中找到该子数组,其值为“ 2TestCamp2_1482149078243”; 然后从该子数组获取键id和ext id的值。

谢谢。 如果有人在这方面帮助我,那将很好。 我是java和json的新手。

String validate_name="2TestCamp2_1482149078243";

JSONObject object = new JSONObject(YOUR-JSON-Object);
JSONArray getArray = object.getJSONArray("Result");

for(JSONObject value:getArray)  //For Each loop to iterate the Objects from JSONArray
{
    if(value.getString("name").equals(validate_name))
    {
        value.getString("id");
        value.getString("extId");
    }
}

暂无
暂无

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

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