0

I have this format of code. Can anyone help me how can I read the array? I need to read contacts array.

 {
  "success": true,
  "contacts": [
  {
  "username": "ceema",
  "profile": {
    "name": "Ceema S",
    "email": "ceemas@gmail.com",
    "address": "No 143, gangaeyam, Maikkad P O, Kerala",
    "phone": {
      "mobile": "3333",
      "home": "2222",
      "office": "6666"
    }
  }
},
{
  "username": "hari",
  "profile": {
    "name": "Harikrishnan V S",
    "email": "harikriii@gmail.com",
    "address": "No 143, harihome, Maikkad P O, Kerala",
    "phone": {
      "mobile": "3333",
      "home": "2222",
      "office": "6666"
    }
  }
},
{
  "username": "pranav",
  "profile": {
    "name": "Pranav Mohan",
    "email": "pranavmohan@gmail.com",
    "address": "No 143, harihome, Maikkad P O, Kerala",
    "phone": {
      "mobile": "3333",
      "home": "2222",
      "office": "6666"
    }
  }
}
]
}

I tried this :

if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    //String id = c.getString(TAG_NAME);
                    String name = c.getString(TAG_NAME);
                    String email = c.getString(TAG_EMAIL);
                    String address = c.getString(TAG_ADDRESS);
                    //String gender = c.getString(TAG_GENDER);

                    // Phone node is JSON Object
                    JSONObject phone = c.getJSONObject(TAG_PHONE);
                    String mobile = phone.getString(TAG_PHONE_MOBILE);
                    String home = phone.getString(TAG_PHONE_HOME);
                    String office = phone.getString(TAG_PHONE_OFFICE);

This jsonstr is json response from the site. I need to read the nested contacts and populate it to

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Jeet
  • 115
  • 2
  • 13

3 Answers3

1

This jsonstr is json response from the site. I need to read the nested contacts and populate it to

you got it almost right. The objects you are looking for are inside profile

JSONObject c = contacts.getJSONObject(i);
JSONObject profile = c.getJSONObject("profile");

and use profile instead of c, to look of name, phone and the others

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

check this link a very simple way to parse json http://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON

0

The simplest way is to get POJO class from json using http://www.jsonschema2pojo.org/

You will get all the required classes to convert this json into POJO class, and later you can use Gson library from google to convert json into Object and the other way around.

Think of this, if you get a Contact class as parent class then you can easily use fromjson method from Gson.

Gson gson=new Gson();
Contact contact = gson.fromJson(json_string, Contact.class);

Now you can easily use this class for further use. Hope this helps.

Vikas Mane
  • 789
  • 1
  • 6
  • 14