org.json.JSONObject cannot be converted to JSONArray

前端 未结 3 1692
隐瞒了意图╮
隐瞒了意图╮ 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: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.

提交回复
热议问题