简体   繁体   中英

parsing JSONArray with multiple values in android

I have a JSONfile like :

 { "data" :
     { "stop1" : [ "elem1", "elem2", "elem3", "elem4"],    
       "stop2" : [ "selem1", "selem2", "selem3", "selem4"] 
     } 
 }

For parsing this file I have written the following code:-

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));

However i get an error as:

org.json.JSONException: Value {"stop1":["elem1","elem2"
org.json.JSON.typeMismatch(JSON.java:100) org.json.JSONArray.getJSONArray(JSONArray.java:500)

Seems like your JSON is invalid. The correct JSON should be:

{"data": {
    "stop1": [
      "elem1",
      "elem2",
      "elem3",
      "elem4"
    ],
    "stop2": [
      "selem1",
      "selem2",
      "selem3",
      "selem4"
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM