-4

Hi I am trying to get JSON from url to ListItem where I want to show it. But I am getting error like:

json parsing error: value 48600123456 at 0 of type java.lang.String cannot be converted to JSONObject.

There is part of code:

@Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

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

                    // Getting JSON Array node
                    JSONArray contacts = jsonObj.getJSONArray("phones");

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

                       String name= c.getString("name");
                       String phone = c.getString("phone");
                       String phone2 = c.getString("phone2");

                        // tmp hash map for single contact
                        HashMap<String, String> contact = new HashMap<>();

                        // adding each child node to HashMap key => value
                        contact.put("name", name);
                        contact.put("phone", phone);
                        contact.put("pone2", phone2);

                        //adding contact to contact list
                        contactList.add(contact);
                    }

And there is my JSON.

{"phones":["phone: 48600123456","phone2: 48600234532","phone3: 48600567234"]}

What is wrong?

Defus
  • 789
  • 2
  • 12
  • 22

4 Answers4

2

The method fails at the first iteration of

JSONObject c = contacts.getJSONObject(i);

because, according to the json given in input, the variable contacts is an array of String ("48600123456","48600234532","48600567234"), not of JSONObject

This is the way to parse the given json:

...

// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("phones");

// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
    String value = contacts.getString(i);

    ...
}
TechIsFun
  • 21
  • 3
0

You need to index the array by the correct type. Each value is a string, so use getString. Otherwise, you just use get, and cast the Object later.

Honestly, I think you should be using just a List for the phones, not a map, but this is the correct code that you're trying to do

// Getting JSON Array node
JSONArray phones = jsonObj.getJSONArray("phones");

// tmp hash map for single contact 
Map<String, String> phoneMap = new HashMap<>();

// adding each phone to HashMap key => value 
for (int i =0; i < phones.getLength(); i++) {
    phoneMap.put("phone"+i, phones.getString(i));
} 

//adding phones to contact list
contactList.add(phoneMap);

Might be worth it to actually define a Contact object. Then you can try using Gson, for example, to parse the JSON for you

EDIT

"name: value" is still one string. If you want an array of JSON objects, you must have {} on the outside, and keys need [{"phone": "48600123456", "phone2": "48600234532"}]

If you format it, you can clearly see the difference.

{
    "phones": ["phone: 48600123456", "phone2: 48600234532", "phone3: 48600567234"]
}

vs

{
    "phones": [{
        "phone": "48600123456",
        "phone2": "48600234532",
        "phone3": "48600567234"
    }]
}

vs

{
    "phones": [{
            "phone": "48600123456"
        },
        {
            "phone": "48600234532"
        },
        {
            "phone": "48600567234"
        }
    ]
}

Personally, I see nothing wrong with the first way you posted, which is what the code above assumes

{
    "phones": ["48600123456", "48600234532", "48600567234"]
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

You don't have any JsonObject inside your jsonarray, that you are trying to parse in your code here

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

Access your strings directly from your array like this

for (int i = 0; i < contacts.length(); i++) {
      String contact=contacts.getString(i);
    }
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
-1

From your code, I think your json structure is such as :

{"phones":[ {"name":"48600123456", "phone":"48600234532", "phone2":"48600567234"}]}

CoXier
  • 2,523
  • 8
  • 33
  • 60