-5

I want to read all data from below received json format.how to read it. i want to get value to user_id,user_mobile and so on.

[
    {
       "message": {
          "user_id": 1012761333,
          "user_type": "Main_Vol",
          "application_no": "CVFIV1012761333",
          "user_aadhar": "233439999993",
          "user_mobile": "7344465899",
          "user_email": "pghhff@ggmail.com",
          "user_name": "Xxxx",
          "user_dob": "20/11/2017",
          "user_father_name": "A",
          "user_mother_name": "C",
          "user_address": "A",
             "user_city": {
                "city_id": 1,
                "city_name": "Pitam Pura",
                "state_id": "Delhi"
             },
             "user_state": "Delhi",
             "user_pincode": "987654",
             "user_payment_status": 0,
             "user_created_by": 1012761333,
             "user_created_on": "Dec 21, 2017 3:17:34 PM"
       }
   }
]
Ankesh Roy
  • 278
  • 2
  • 8
  • 33

2 Answers2

1

You have json array so you need to read like below

  try {

        JSONArray jsonArray=new JSONArray("your data");

        if(jsonArray.length()>0){

            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);

                JSONObject messageObj=jsonObject.getJSONObject("message");
                String user_id=messageObj.getString("user_id");
                String user_type=messageObj.getString("user_type");
                ----and so on.

            }
        }
      } catch (Exception e) {
        e.printStackTrace();
    }
sunil
  • 796
  • 1
  • 8
  • 28
0

Try this

try {
            JSONArray jsonArray = new JSONArray("response");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                JSONObject message = jsonObject.getJSONObject("message");
                String user_id = message.getString("user_id");
                String user_mobile = message.getString("user_mobile");
                String user_type = message.getString("user_type");
                String application_no = message.getString("application_no");
                String user_aadhar = message.getString("user_aadhar");
                String user_email = message.getString("user_email");
                String user_name = message.getString("user_name");
                String user_dob = message.getString("user_dob");
                String user_father_name = message.getString("user_father_name");
                String user_address = message.getString("user_address");
                String user_mother_name = message.getString("user_mother_name");

                JSONObject user_city = jsonObject.getJSONObject("user_city");
                String city_id = user_city.getString("city_id");
                String city_name = user_city.getString("city_name");
                String state_id = user_city.getString("state_id");



                String user_state = message.getString("user_state");
                String user_pincode = message.getString("user_pincode");
                String user_payment_status = message.getString("user_payment_status");
                String user_created_by = message.getString("user_created_by");
                String user_created_on = message.getString("user_created_on");





            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • my response is of Bean type not String type that's why i am not able to get all value using loop.i tried it before using loop. – Ankesh Roy Dec 21 '17 at 10:17
  • problem is empty json object of middle one provided in above code – Ankesh Roy Dec 21 '17 at 10:20
  • 2
    "my response is of Bean type" Then create a POJO class and and return its instance form parser . – ADM Dec 21 '17 at 10:42
  • @AnkeshRoy `problem is empty json object of middle one provided in above code` than you need to change your response from back end side – AskNilesh Dec 22 '17 at 06:31