我可以知道如何從json數組中獲取JSON對象嗎?從json數組中獲取對象
JSON:
[
{
"id": 1,
"region": "Ilocos Region (Region I)",
"province": "Ilocos Norte",
"city": "Laoag City"
},
{
"id": 2,
"region": "Ilocos Region (Region I)",
"province": "Ilocos Norte",
"city": "Batac City"
},
{
"id": 3,
"region": "Ilocos Region (Region I)",
"province": "Ilocos Sur",
"city": "Vigan City"
}
]
我可以得到該地區,省,市,但我不能得到的ID。
這裏是我使用的代碼:
try {
br = new BufferedReader(new FileReader(path+"json/src_city.json"));
try {
while ((inputline = br.readLine()) != null) {
JSONArray a = (JSONArray) parser.parse(inputline);
for (Object o : a) {
JSONObject sample = (JSONObject) o;
id = (int) sample.get("id");
}}
我找到了我的問題的答案,它被解釋得很好。 http://stackoverflow.com/a/31194430/1840521 – LyodMichael