1

Hello guys this is my json response:

 [
  {
  "Woa": [
    "Seo",
    "Rikjeo",
    "JDa"
   ]
  },
 "Aha",
 "Aad",
 "Char"
 ]

I want to add to list strings of Woa:

{
  "Woa": [
    "Seo",
    "Rikjeo",
    "JDa"
   ]
  }

This is what I've done so far:

JSONObject object = new JSONObject(result);
            JSONArray a = object.getJSONArray("Woa");
            for (int i = 0; i < a.length(); ++i) {
                listWoa.add(a.getString(i));
            }

But I'm getting this error:

type org.json.JSONArray cannot be converted to JSONObject

Any ideas why I'm not getting any string and cannot be converted to JSONObject.

10
  • Your response is in JSONArray not in JSONObject Commented Jul 21, 2015 at 9:22
  • So how do I solve this? Commented Jul 21, 2015 at 9:24
  • object is an array first. so use object[0].getJSONArray("Woa"); Commented Jul 21, 2015 at 9:24
  • parse json with JSONArray Commented Jul 21, 2015 at 9:25
  • @sanatshukla how do I get inside array? Commented Jul 21, 2015 at 9:28

4 Answers 4

3

To parse above json response. Try below code:

        JSONArray jsonArray = new JSONArray(result);
        JSONObject jsonObjWoa = jsonArray.getJSONObject(0);
        JSONArray jsonArrayWoa = jsonObjWoa.getJSONArray("Woa");
        for (int i = 0; i < jsonArrayWoa.length(); ++i) {
            listWoa.add(jsonArrayWoa.getString(i));
        }
Sign up to request clarification or add additional context in comments.

Comments

2

Your JSON is an array (a list of "things"), where the first item is an object

 [                             << This is an array (let's call it A)
  {                                 << This is A[0]
  "Woa": [
    "Seo",
    "Rikjeo",
    "JDa"
   ]
  },
 "Aha",                             << This is A[1] == "AhA"
 "Aad",                             << This is A[2] == "aad"
 "Char"                             << This is A[3] == "Char"
 ]

And thus, A[0] is the object :

  {                           << This is an object ( A[0] )
  "Woa": [                       << This is A[0].Woa (it's an array)
    "Seo",                          << This is A[0].Woa[0] == "Seo"
    "Rikjeo",                       << This is A[0].Woa[1] == "Rikjeo"
    "JDa"                           << This is A[0].Woa[2] == "JDa"
   ]
  }

The easy way to not mix arrays and objets in JSON is this :

[...]    is an array
{...}    is an object

Comments

1

Do something like this:

JSONArray jsonArray = new JSONArray(result);
JSONObject object = new JSONObject(jsonArray.get(0).toString());// if you have only one element then get 0th index
JSONArray a = object.getJSONArray("Woa");
 for (int i = 0; i < a.length(); ++i) {
      listWoa.add(a.getString(i));
  }

1 Comment

I'm getting cannot resolve constructor in new JSONObject(jsonArray.get(0));
0

From my point of view: I would use GSON to auto parse the JSON and this site to generate POJOs.

http://www.jsonschema2pojo.org/ make sure to click: GSON as Annotation style, and use JSON instead of JSON Scheme

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.