org.json.JSONArray cannot be converted to JSONObject

前端 未结 5 1355
滥情空心
滥情空心 2020-12-01 16:22

I am new to JSON and I am getting the follwoing Exception:

org.json.JSONArray cannot be converted to JSONObject in the first line of try se

5条回答
  •  既然无缘
    2020-12-01 16:47

    This

    JSONObject json = new JSONObject(strResponse);
    // your strResponse is a json array 
    

    should be

    JSONArray jsonarray = new JSONArray(strResponse);
    

    [ represents json array node

    { represents json object node

    for(int i=0; i < jsonarray.length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        String id       = jsonobject.getString("id");
        String title    = jsonobject.getString("title");
        String company  = jsonobject.getString("company");
        String category = jsonobject.getString("category");
    }
    

提交回复
热议问题