安卓中多值java解析JSONArray
我有一个JSONfile,比如:
{ "data" :
{ "stop1" : [ "elem1", "elem2", "elem3", "elem4"],
"stop2" : [ "selem1", "selem2", "selem3", "selem4"]
}
}
为了解析这个文件,我编写了以下代码:-
HashMap<String, String> stopList = new HashMap<>();
JSONObject dataJsonObj = (new JSONObject(json)).getJSONObject("data");
JSONArray busStopJsonObj, urlJsonObj;
busStopJsonObj = dataJsonObj .getJSONArray("stop1");
urlJsonObj = dataJsonObj .getJSONArray("stop2");
for (int i = 0; i < busStopJsonObj.length(); i++)
stopList.put(busStopJsonObj.getString(i), urlJsonObj.getString(i));
然而,我得到一个错误:
org.json.JSONException: Value {"stop1":["elem1","elem2"
org.json.JSON.typeMismatch(JSON.java:100) org.json.JSONArray.getJSONArray(JSONArray.java:500)
# 1 楼答案
看来你的JSON是无效的。正确的JSON应该是: