1
建立一個嵌套的JSON,我試圖建立以下類型內循環
[
{"Value":0,"Cities":["London","Tokyo","Washington"],"Name":"John"},
{"Value":1,"Cities":["London","Tokyo","Washington"],"Name":"John"},
{"Value":2,"Cities":["London","Tokyo","Washington"],"Name":"John"}
]
的JSON對象,這是我到現在爲止完成。 JSON對象用最後一項寫入。
JSONArray outer = new JSONArray();
JSONObject js = new JSONObject();
List<String> str = new ArrayList<String>();
str.add("London");
str.add("Tokyo");
str.add("Washington");
int res = 3;
for(int i=0;i<res;i++){
js.put("Value", i);
js.put("Name", "John");
js.put("Cities", str);
outer.add(js);
}
System.out.println(outer);
什麼我現在得到的是:
[
{"Value":2,"Cities":["London","Tokyo","Washington"],"Name":"John"},
{"Value":2,"Cities":["London","Tokyo","Washington"],"Name":"John"},
{"Value":2,"Cities":["London","Tokyo","Washington"],"Name":"John"}
]
是的,這是一個虛擬的例子。我現在明白了這個問題。謝謝。 – Betafish