错误解析JSON响应字符串的Andr​​oid [英] Error Parse JSON Response String Android

查看:140
本文介绍了错误解析JSON响应字符串的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的API服务的响应字符串:

  {ID:登录,状态:真正的}

和这是解析响应字符串从主要的状态

获得价值的方式

 的JSONObject jsonObj = NULL;
    尝试{
        jsonObj =新的JSONObject(responseString);
    }
    赶上(JSONException E){
        e.printStackTrace();
    }
        JSONArray innerJsonArray = NULL;
        尝试{
            innerJsonArray = jsonObj.getJSONArray(状态);
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        的JSONObject的JSONObject = NULL;
        尝试{
            的JSONObject = innerJsonArray.getJSONObject(0);
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        尝试{
            的System.out.println(jsonObject.getString(身份));
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }

和我有错误
org.json.JSONArray不能转换为JSONObject的

任何人都可以给我建议?


解决方案

 的JSONObject jsonObj = NULL;
尝试{
    jsonObj =新的JSONObject(responseString);
    的System.out.println(jsonObj.getString(身份));
}
赶上(JSONException E){
    e.printStackTrace();
}

您不需要与任何其他阵列打扰。

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?

解决方案

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.

这篇关于错误解析JSON响应字符串的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆