Value of type org.json.JSONArray cannot be converted to JSONObject

后端 未结 2 1578
南旧
南旧 2021-02-13 17:56

Got stuck at this error:

3169-3190/com.meisolsson.app E/JSON Parser﹕ Error parsing data org.json.JSONException: Value [{\"type\":0,\"can_see_custom_stori

相关标签:
2条回答
  • 2021-02-13 18:44

    [..] means it should be an JSONArray and {..} means it should be a JSONObject.

    Therefore:

    try {
            JSONArray jObj = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
    
    0 讨论(0)
  • 2021-02-13 18:50

    Even though I had a single JSON object it was stored in an array as Hariharan has pointed out with the square brackets [ { items:items, ... } ]

    My solution was simply to parse out the only object, located in array position [0] like this

    JSONArray jsonArray = new JSONArray(stringIn);
    
    JSONObject obj = jsonArray.getJSONObject(0);
    
    0 讨论(0)
提交回复
热议问题