0

I have a question about how parsing json file. My json structure is like this:

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "services": [
                   "laundry",
                   "wifi",
                   "tv",
                   "swimming pool",
                   "bar"
                 ],
                "phone": [
                    910000000000,
                    00000000,
                    000000
                ]
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "services": [
                   "laundry",
                   "wifi",
                   "tv",
                   "swimming pool",
                   "bar"
                 ],
                "phone": [
                    0000000000,
                    00000000,
                    00000000
                ]
        }
  ]

How can I get the phone values and the services values?

phones = jsonObj.getJSONArray(TAG_PHONE);
for (int x = 0; x < phones.length(); x++) {

}

because to get the ID for example I haven´t got problems:

for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
// tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ID, id);
contactList.add(contact);

Thank you very much

2 Answers2

1

Phone and Services are JSONArray objects, so when you do the get function, you should use a .getJSONArray()

Ex:

JSONArray phoneArray = c.getJSONArray("phone");
for(int i=0;i<phoneArray.length();i++){
    JSONObject json_phone_data = phoneArray.getJSONObject(i);
    String phone_data = phoneArray.getString(i);
    // Do something with phone data
}

JSONArray servicesArray = c.getJSONArray("services");
for(int i=0;i<servicesArray.length();i++){
    JSONObject json_services_data = servicesArray.getJSONObject(i);
    String services_data = servicesArray.getString(i);
    // Do something with services data
}

See documentation.

erad
  • 1,766
  • 2
  • 17
  • 26
  • Thank you so much, and how can I get the string value of json_data i element? Thanks :) – Jon Arruabarrena Sep 30 '14 at 13:28
  • Well if you want the String, you can simply use `String json_phone_data = phoneArray.getString(i);` – erad Sep 30 '14 at 13:31
  • And in the list Adapter how can I do it? TextView phone = (TextView)vi.findViewById(R.id.telefono_alojamiento); HashMap contact = new HashMap(); contact = data.get(position); phone.setText(song.get(MyFragment.TAG_PHONE))); – Jon Arruabarrena Sep 30 '14 at 14:44
  • Without seeing your full code, I cannot say for certain. But that's not really how you use HashMaps. Typically you do something like `contact.put("key", value);`. Additionally, your setText could be right as long as `song.get(MyFragment.TAG_PHONE)` is a value that can be implicitly converted to a String. Typically, people like to use `.getString()` wherever possible. Also, if this solved the issue for you, please vote up and indicate it as the solution. – erad Sep 30 '14 at 15:05
1

Services and Phone are inner JSONArray of Contacts. So from the contacts JSONObject you can use their key to retrieve the respective object and loop upon

for (int i = 0; i < contacts.length(); i++) {
    JSONObject c = contacts.getJSONObject(i);
    JSONArray phone = c.optJSONArray("phone")
    if (phone != null) {
       for (int x = 0; x < phones.length(); x++) {
            Log.i("PHONE", "phone at #" + x + " " + phone.optInt(x));
        }   
    }

    JSONArray services = c.optJSONArray("services"); 
    if (services != null) {
      for (int j = 0; j < services.length(); j++) {
            Log.i("SERVICE", "service at #" + j + " " + services.optString(j));
      } 
    }
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305