0

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

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "[email protected]",
                "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": "[email protected]",
                "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

2 Answers 2

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.

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

4 Comments

Thank you so much, and how can I get the string value of json_data i element? Thanks :)
Well if you want the String, you can simply use String json_phone_data = phoneArray.getString(i);
And in the list Adapter how can I do it? TextView phone = (TextView)vi.findViewById(R.id.telefono_alojamiento); HashMap<String, String> contact = new HashMap<String, String>(); contact = data.get(position); phone.setText(song.get(MyFragment.TAG_PHONE)));
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.
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));
      } 
    }
}

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.