如何从java中的子数组中的值中提取json数组的子集?

时间:2017-02-05 14:11:38

标签: java json

以下是我的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数组开始,首先我想找到那个子数组,其中value是“2TestCamp2_1482149078243”;然后从该子数组中获取密钥id和ext id的值。

感谢。如果有人帮助我,那会很棒。我是java和json的新手。

1 个答案:

答案 0 :(得分:0)

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");
    }
}