1

-- I know parsing, i have successfully parsed below data, but when web-service is upgraded i need to parse object inside object. - I tried many example but getting error.

Code for Parsing :

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;
    }

Error Log :

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"
        }
    }
5
  • you have used some Keys whose values are unknown like KEY_SUCCESS, KEY_DATA . please add those values and formate your code so that is easy to read Commented May 14, 2016 at 5:31
  • Your json missing ] } at the end of file Commented May 14, 2016 at 5:32
  • @Dixit Panchal, its not missing, mistake in copy paste Commented May 14, 2016 at 5:32
  • JsonArray jsonArray=new jsonArray(jsonObject.getString(""data)) than JsonObject newObj=jsonArray.getJsonObject(position); Commented May 14, 2016 at 5:33
  • @Shubhank, i have updated my question Commented May 14, 2016 at 5:34

2 Answers 2

1

You dont need this line

   JSONObject objectDetails2 = obj.getJSONObject(KEY_DATA);

and change

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

to

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

As you can see from the 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

Sign up to request clarification or add additional context in comments.

7 Comments

JSONObject businessObject = jsonArrayUser.getJSONObject(j); What i have to do with this
first i would suggest to go to jsonlint.com and paste your json. it will process and show it indented with line break. edit your question and paste that so it is easy to explain
can u plz help me with my code .. proper output. plz
what is the output you want ?
i am getting object data successfully, but i want object userdata(everything inside userdata)
|
0

I notice that your JSON has some errors. is this the complete Json response?

Because JSON response should be something like this:

{
    "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"
        }
    }]
}

You will notice that there is no close for [ after userdata in your Json.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.