1

This is a json response that i'm unable to parse. Please help me to parse this response.

{ "status": "true", "data": { "userinfo": [ { "id": "77", "firstname": "Test", "lastname": "" } ] } }

And this is my android code.

protected Void doInBackground(Void... params) {

 ServiceHandler2 sh = new ServiceHandler2();

 String url="http://192.168.0.65/hostandguest/android/viewprofile?uid=77";

 String jsonStr = sh.makeServiceCall(url, ServiceHandler2.GET);

 Log.d("Response: ", "> " + jsonStr);

  if (jsonStr != null) {

    try {

         JSONObject jsonObj = new JSONObject(jsonStr);
         String status = jsonObj.getString("status");

          if (status.equalsIgnoreCase("true")) {

            JSONArray   Userarr=jsonObj.getJSONArray("userinfo");
            String  data=jsonObj.getString("data");
             for(int i=0;i<Userarr.length();i++)
              {
                JSONObject c = Userarr.getJSONObject(i);
                String Fname=c.getString("firstname");
                Fname_arr.add(Fname);
                Log.v("Fname",Fname);                   
              }
            }
            else {
                  Log.v("Status", "false");
                 }

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

        return null;
    }
2
  • It's always usefull to post the error it returns. Commented Sep 24, 2015 at 13:24
  • Did you getting any response?? if yes then you can check my answer. it will work for you. Commented Sep 24, 2015 at 13:35

3 Answers 3

1

get data josnObject first as:-

JSONArray   Userarr=jsonObj.getJsonObject("data").getJSONArray("userinfo");
Sign up to request clarification or add additional context in comments.

Comments

1

JSONArray Userarr=jsonObj.getJSONArray("userinfo"); would require jsonObj to contain the array "userinfo". This is not the situation you have presented. That array is inside a json sub object named "data".

        JSONObject jsonSubObj = jsonObj.getJSONObject("data");
        JSONArray  Userarr=jsonSubObj.getJSONArray("userinfo");

should be a fix. I've not checked for other errors.

Comments

0

You are getting first userinfo object and then after data it's wrong.

I had changed like the following way you have to do this thing.

userinfo is sub array of data object.

protected Void doInBackground(Void... params) {

        ServiceHandler2 sh = new ServiceHandler2();

        String url = "http://192.168.0.65/hostandguest/android/viewprofile?uid=77";

        String jsonStr = sh.makeServiceCall(url, ServiceHandler2.GET);

        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {

            try {

                JSONObject jsonObj = new JSONObject(jsonStr);
                String status = jsonObj.getString("status");

                if (status.equalsIgnoreCase("true")) {

                    JSONObject dataObj = jsonObj.getJSONObject("data");
                    JSONArray infoObj = dataObj.getJSONArray("userinfo");

                    for (int i = 0; i < infoObj.length(); i++) {
                        JSONObject c = Userarr.getJSONObject(i);
                        String Fname = c.getString("firstname");
                        Fname_arr.add(Fname);
                        Log.v("Fname", Fname);
                    }
                } else {
                    Log.v("Status", "false");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

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.