我有一個json數組有四個對象。Comapring Json Array的對象並顯示具有相同參數的對象
{"name":"Ram", "email":"[email protected]", "age":23},
{"name":"Shyam", "email":"[email protected]", "age":28},
{"name":"John", "email":"[email protected]", "age":23},
{"name":"Bob", "email":"[email protected]", "age":41}
我想遍歷json對象並顯示具有相同年齡的人的詳細信息。我正在使用Java。 我是json的新手。任何人都可以提出如何繼續。我正在使用json.org庫。
我已經來到這麼遠:
public static void main(String[] args) {
String text="{\"name\":\"Ram\", \"email\":\"[email protected]\", \"age\":23}"
+ "{\"name\":\"Shyam\", \"email\":\"[email protected]\", \"age\":28}"
+ "{\"name\":\"John\", \"email\":\"[email protected]\", \"age\":23}"
+ "{\"name\":\"Bob\", \"email\":\"[email protected]\", \"age\":41}";
String changed=text.toString();
int pos=changed.lastIndexOf("}");
String change=changed.substring(0,pos);
change=change.replace("}", "},");
String res=change.concat("}");
String msg="["+res+"]";
JSONArray json=new JSONArray(msg);
for(int i=0;i<json.length();i++){
JSONObject obj= json.getJSONObject(i);
}
}
[Parse JSON with org.json]的可能重複(https://stackoverflow.com/questions/6442347/parse-json-with-org-json) –
你到目前爲止嘗試過什麼?請給我們看一些代碼。你的json也是無效的。檢查它在這裏:https://jsonlint.com/ – JanTheGun
我已經解析了數組來獲取每個對象。我想知道的是如何在這種情況下進行,我將使用有效的json,但我無法理解如何遍歷整個json數組並比較不同的年齡值。我來了這麼遠: – Amit