org.json.JSONObject cannot be converted to JSONArray

前端 未结 3 1679
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 13:35

I am getting a exception while working in json . My JSONPresr class is as follow

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io         


        
相关标签:
3条回答
  • 2020-12-06 14:20

    Learn from exception

    org.json.JSONObject cannot be converted to JSONArray
    

    You are getting this exception org.json.JSONObject cannot be converted to JSONArray because you are trying to convert JSONObject to JSONArray which is not possible.

    { represents json object node

    [ represents json array node

    0 讨论(0)
  • 2020-12-06 14:27

    your jsondata is json object format. change this line

    JsonObject user_detail_jsonobj = json.getJSONObject(TAG_user_detail);
    
    0 讨论(0)
  • 2020-12-06 14:32

    Its clear from error that you are trying to convert Json Object into Json array. That should not.

    Here is the code to read your JSON response.

    String json = "Assuming that here is your JSON response"; 
    try {
        JSONObject parentObject = new JSONObject(json);
        JSONObject userDetails = parentObject.getJSONObject("user_details"); 
    
        //And then read attributes like             
        String name = userDetails.getString("user_name"); 
        String phone = userDetails.getString("user_phone");
        String id = userDetails.getString("re‌​f_id");
    
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    

    Above code is for {"user_details":{"user_id":"1","user_name":"chand","user_phone":"9620085675","re‌​f_id":6386}} JSON.

    0 讨论(0)
提交回复
热议问题