2017-02-05 29 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"); 
    } 
}