Error Parse JSON Response String Android

2019-09-02 02:26发布

I have a response String from an API service like this :

{"id":"login","status":"true"}

and This is the way to parse Response String to get Value from Key "Status"

                    JSONObject jsonObj = null;
    try{
        jsonObj = new JSONObject(responseString);
    }
    catch(JSONException e){
        e.printStackTrace();


    }


        JSONArray innerJsonArray = null;
        try {
            innerJsonArray = jsonObj.getJSONArray("status");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = innerJsonArray.getJSONObject(0);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            System.out.println(jsonObject.getString("status"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

and I've got error "org.json.JSONArray cannot be converted to JSONObject"

Anyone can give me suggestion?

3条回答
干净又极端
2楼-- · 2019-09-02 02:34

In which line you get the exception ? In any way may be you should use

jsonObj =JSONObject.fromObject(responseString);

查看更多
等我变得足够好
3楼-- · 2019-09-02 02:37

Kindly try the snippet code below. Try this link for gaining better knowledge about parsing an JSON response.

JSONObject jObj;
        try {
            jObj = new JSONObject(responseString);
            Log.i("id==", jObj.getString("id"));
            Log.i("status==", jObj.getString("status"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
查看更多
Rolldiameter
4楼-- · 2019-09-02 02:55
JSONObject jsonObj = null;
try{
    jsonObj = new JSONObject(responseString);
    System.out.println(jsonObj.getString("status"));
}
catch(JSONException e){
    e.printStackTrace();
}

You do not need to bother with any other arrays.

查看更多
登录 后发表回答