java/Android error系列01: Value of type org.json.JSONObject cannot be converted to JSONArray

在使用官方提供的:JSONObject来解析json数据时报错:Value of type org.json.JSONObject cannot be converted to JSONArray

一般得到的json返回数据:jsonData解析如下::

    private void parseJSONWithJSONObject(String jsonData){
        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(jsonData);
            for(int i=0; i<jsonData.length();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String id = jsonObject.getString("id");
            String name = jsonObject.getString("name");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        }
    }

>>> 

分析原因为:当返回我的json数据仅为一个时(如简单的登录信息)例如:{“name”:”aaa”,”id”:12},不能惯用思维利用JSONArray进行解析,而应该是:JSONObject!上述的惯用方法应是对有大量重复类型的json object的解析。不能惯性思维。

JSONObject jsonObject = new JSONObject(responsebody);
status = jsonObject.getString("status");
message =jsonObject.getString("msg");

觉得好点个赞呦~

猜你喜欢

转载自blog.csdn.net/lucasxu01/article/details/80764114