org.json.JSONException:类型org.json.JSONObject无法转换为JSONArray

时间:2017-06-20 18:13:00

标签: android json

响应是这样的,我希望类型&来自这个Json字符串的消息,我不知道如何做到这一点?:

来自服务器的响应

{
"Response": {
    "status": {
        "type": "Success",
        "message": "You are authorized to access"
    },
    "data": {
        "msg": "Data Found",
        "user": {
            "user_id": "1",
            "user_full_name": "User",
            "user_name": "Username"
        }
    }
}
}

错误:

   org.json.JSONException: Value {"Response":{"status":{"type":"Success","message":"You are authorized to access"},"data":{"msg":"Data Found","user":{"user_id":"1","user_full_name":"User","user_name":"username"}}}} of type org.json.JSONObject cannot be converted to JSONArray

代码

    String resp = response.body().string();
                        JSONArray arr = null;
                        try {
                            arr = new JSONArray(resp);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        JSONObject jObj = null;
                        try {
                            jObj = arr.getJSONObject(0);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        try {
                            String type = jObj.getString("type");
                            String msg = jObj.getString("message");
                            if(type == "Success"){
                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                startActivity(intent);
                            }else{
                                Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

我想找回按摩&来自这个json的状态。有人请帮忙。

2 个答案:

答案 0 :(得分:0)

/**This part of code works to get value of type & message**/

String resp = response.body().string();

try
{
   JSONObject resp_jsonObject = new JSONObject(resp);

   String responseString = resp_jsonObject.getJSONObject("Response");

   JSONObject response_jsonObject = new JSONObject(responseString);  

   String statusString = response_jsonObject.getJSONObject("status"); 

   JSONObject status_jsonObject = new JSONObject(statusString);

   String type = login_jsonObject.getString("type");

   String message = login_jsonObject.getString("message");

}
catch (JSONException e)
{
   e.printStackTrace();
}

答案 1 :(得分:0)

试试这个,

try {
    JSONObject obj=new JSONObject(result);
    JSONObject obj_response=obj.getJSONObject("Response");
    JSONObject obj_status=obj_response.getJSONObject("status");

    String type=obj_status.getString("type");
    String message=obj_status.getString("message");

    Log.d("TAG"," type:"+type+" message:"+message);

} catch (JSONException e) {
    e.printStackTrace();
}