user866364
user866364

Reputation:

How to convert string json to JSONArray?

How can I convert this string into JSONArray ?

{"result":{"passion":[{"id":2,"description":"Sushi"},{"id":3,"description":"Dinner"},{"id":4,"description":"Sex"},{"id":5,"description":"Boobies"},{"id":6,"description":"Sleep"},{"id":7,"description":"Cars"},{"id":8,"description":"Travel"},{"id":9,"description":"Soccer"},{"id":10,"description":"Silice"}]}}

I'm trying to do:

 JSONArray jsonArray = jsonObject.getJSONArray("passion");

But I am getting an exception:

org.json.JSONException: No value for passion

Upvotes: 0

Views: 223

Answers (3)

Meenal
Meenal

Reputation: 2877

Use this code

 JSONObject jsonObject = new JSONObject(json);
 JSONObject resultValues = jsonObject.getJSONObject("result"); 
 JSONArray passionArray = resultValues.getJSONArray("passion")

Upvotes: 0

is seems like you are not getting result object

JSONArray jsonArray = jsonObject. getJSONObject("result").getJSONArray("passion");

Upvotes: 2

Bhanu Sharma
Bhanu Sharma

Reputation: 5145

may be this is what you need dear

ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray();
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
    try {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        stringArray.add(jsonObject.toString());
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions