2017-02-05 30 views
0

以下是我的JSON:如何从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,我想ID和地方的名字是 “2TestCamp2_1482149078243” EXTID。我怎样才能得到这个在JAVA?

一般来说,从一个json数组中,首先我想找到那个子数组,其中值是“2TestCamp2_1482149078243”;然后从该子数组中获取密钥id和ext id的值。

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

+1

的【如何解析JSON在Java中(http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) –

+0

通过子阵列,你的意思是可能的复制JSON对象正确吗?你是否已经在操纵'JsonArray'?您可能希望在您发布的JSON周围添加'[]',因为它当前不是有效的JSON值。 – Aaron

+0

是的,正确的亚伦。我错过了加入[]。 – Nikita

回答

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