Json 解析对象内部的对象出错

Json Parsing object inside Object getting error

-- 我知道解析,我已经成功解析了以下数据,但是当网络服务升级时,我需要解析对象内部的对象。 - 我尝试了很多示例但出现错误。

解析代码:

private final String KEY_SUCCESS = "status";
    private final String KEY_MSG = "Message";
    private final String KEY_MSG1 = "Message";
    //   private final String KEY_AddressList = "addressList";
    private final String KEY_USERINFO = "user_info";
    ArrayList<HashMap<String, String>> arraylist;

    private final String KEY_DATA = "data";
    private final String KEY_USERDATA = "userdata";

public ArrayList<HashMap<String, String>> getList(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getString(KEY_SUCCESS).equals("true")) {

                arraylist = new ArrayList<HashMap<String, String>>();
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                for (int i = 0; i < jsonArray.length(); i++) {

                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject obj = jsonArray.getJSONObject(i);
                    //  JSONObject job = obj.getJSONObject("User");

                    Log.d("obj", obj.toString());


                    String user_id = obj.getString(Constants.Params.ID);
                    String createdate = obj.getStringAndyConstants.Params.CREATEDDATE);
                    String postImage = obj.getString(Constants.Params.IMAGE);



                    map.put(AndyConstants.Params.ID, user_id);
                   map.put(AndyConstants.Params.CREATEDDATE, createdate);
                    map.put(AndyConstants.Params.IMAGE, postImage);

   JSONObject objectDetails2 = obj.getJSONObject(KEY_DATA);

                    JSONArray jsonArrayUser = objectDetails2.getJSONArray("userdata");
                    for (int j = 0; j < jsonArrayUser.length(); j++) {
                        HashMap<String, String> mapUser = new HashMap<String, String>();
                        JSONObject businessObject = jsonArrayUser.getJSONObject(j);
                        Log.d("obj", businessObject.toString());
                    }


                    Log.d("map", map.toString());
                    arraylist.add(map);

                }

            }

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

错误日志:

org.json.JSONException: No value for data


E/array: []
E/adapter: []

Json :

{
    "status": true,
    "message": "Successfully add Post",
    "data": [{

        "user_id": "46",
        "image": "",
        "createdate": "2016-05-11 06:05:12",
        "userdata": {
            "first_name": "Alpha",
            "last_name": "Gamma",
            "image": "http:\/\/abcd.net\/abcd\/uploads\/100520161005191462941615032.jpg"
        }
    }

你不需要这条线

   JSONObject objectDetails2 = obj.getJSONObject(KEY_DATA);

并改变

  JSONArray jsonArrayUser = objectDetails2.getJSONArray("userdata");

  JSONObject userData = obj.getJSONObject("userdata");
  String firstName = userData.getString("first_name");
  String lastName  = userData.getString("last_name");

从JSON

可以看出
"data":
     [{"user_id":"46",
        "image":"",
           "createdate":"2016-05-11 06:05:12",
           "userdata":{"first_name

data is having the object [ which denotes an Array while userData shows a { which is a JSONObject

我注意到您的 JSON 有一些错误。这是完整的 Json 回复吗?

因为 JSON 响应应该是这样的:

{
    "status": true,
    "message": "Successfully add Post",
    "data": [{

        "user_id": "46",
        "image": "",
        "createdate": "2016-05-11 06:05:12",
        "userdata": {
            "first_name": "Alpha",
            "last_name": "Gamma",
            "image": "http:\/\/abcd.net\/abcd\/uploads\/100520161005191462941615032.jpg"
        }
    }]
}

您会注意到 Json 中 userdata 之后的 [ 没有收盘价。