wfbarksdale
wfbarksdale

Reputation: 7606

Why is the JSONObject(jsonString) constructor throwing an exception in my android app?

I am trying to parse a JSON object in my android application and I do so like this

JSONObject json = new JSONObject(jsonString);

the value of jsonString is:

  [{"pk": 1, "model": "mydb.user", "fields": {"username": "willyb", "password": "tao1", 
  "signup_date": "2011-11-28 09:15:58", "email": "[email protected]"}}]

Is there an obvious reason why this is failing?

Upvotes: 2

Views: 875

Answers (2)

MByD
MByD

Reputation: 137322

Because this is a JSONArray, not JSONObject. (See here)

You should do:

JSONArray arr = new JSONArray(jsonString);
JSONObject json = arr.get(0);

Upvotes: 2

Chris
Chris

Reputation: 23171

Your string is an array. Try:

JSONArray arr = new JSONArray(jsonString);

Upvotes: 1

Related Questions