1

I have a JSON like this :

{

- 0: {
    "title": "Title1",
    "content": "test"
},
 - 1: {
    "title": "Title2",
    "content": "test2"
}
}

So i want to recover it in my application, and i have an exception with this line :

JSONArray jArray = new JSONArray(result);

I checked that "result" contains my JSON, and it's the case.

When i see the error in the logcat i just see values of my JSON.... i have something like that :

org.json.JSONException and values of my JSON

Could someone help me ?

1
  • can you add complete logcat exception,if possible. Commented Mar 18, 2014 at 10:56

3 Answers 3

2

{ means Object by means if json data starts with a { than it is an object

[ means array by means if json data starts with a [ than it is an array

use

JSONObject jObject = new JSONObject(result);

instead of

JSONArray jArray = new JSONArray(result);

for a simple tutorial and to learn visit

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

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

2 Comments

Ok, i understood but i have a problem because me, i haven't a json object with name, i have just number, not "contacts" like your example. So how can i do a while on jObject ?
Please visit the documentation, you can iterate over the data and findout. you can obtain an object based on a particular key.
1

The JSON you posted is not a JSONArray but a JSONObject so you have to change from

JSONArray jArray = new JSONArray(result);

to

JSONObject jObject = new JSONObject(result);

2 Comments

Oh, ok. But now, how can i do to do a "while" on jObject ?
you still have the possibility to get an iterator with all the keys, do a while on that iterator and retrieve every object through optObject(). I'll strongly suggest you to give a look to the documentation
0

You can get an easy to understand example for encoding and parsing json: Android JSON Tutorial: Create and Parse JSON data

and

JSON in Android - Tutorial

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.