0

Following is my 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"
  }

From above json, I want to get id and extid of where name is "2TestCamp2_1482149078243". How can I get this in JAVA ?

In general, From a json array, first I want to find that sub-array, where value is "2TestCamp2_1482149078243"; and then from that sub-array, get values of keys id and ext id.

Thanks. Would be great if someone helps me in this. I am new to java and json.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58
Nikita
  • 1
  • 1
  • 1
    Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Pavneet_Singh Feb 05 '17 at 14:13
  • By sub-array, you mean JSON object right? Are you already manipulating a `JsonArray` ? You might want to add the `[]` around the JSON you've posted because it currently isn't a valid JSON value. – Aaron Feb 05 '17 at 14:14
  • Yes, Right Aaron. I missed adding [ ]. – Nikita Feb 06 '17 at 05:55

1 Answers1

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");
    }
}
AnilCk
  • 106
  • 3
  • 12