0

I'm in a situation where I have to read the json data and insert it into Sqlite table. But the json data in this format :

{
    "result": "success",
    "data": {
        "userId": "873",
        "volume": "0.5",
        "schoolId": "0",
        "schoolName": "",
        "preferredLanguageId": "1",
        "fname": "robin",
        "lname": "singh",
        "email": "[email protected]",
        "password": "password1111",
        "isParent": "0",
        "countryId": "254",
        "stateId": "143",
        "state": "",
        "city": "san diego",
        "coins": "0",
        "zip": "",
        "players": []
    }
}

JSONObject json = new JSONObject(jsonString);
String uname=json.getString("fname");

But I'm not able to get the first name in the string uname.

1
  • Try JSONArray list = (JSONArray) jsonObject.get("data"); to get collection of all properties nested within the data property. Commented Jul 31, 2013 at 13:15

5 Answers 5

5
String uname=json.getString("fname");

Is not working because the fname property is nested within the data property. Therefore you need to do something like the following:

String uname = json.getJSONObject("data").getString("fname");
Sign up to request clarification or add additional context in comments.

Comments

1
String uname = json.getJSONObject("data").getString("fname");

Comments

0

You have to first get the data property and then get the fname property. fname is inside the data property.

Comments

0

You need to read "data" as a JSON object and then "fname" as a string.

There are many JSON viewers online, this is one of my favorite:

http://jsonviewer.stack.hu/

String uname = json.getJSONObject("data").getString("fname");

Comments

0

First you have to get data object from your JSON string. Here is an example:

JSONObject json = new JSONObject(jsonString);
JSONObject data = json.getJSONObject("data");
String uname = data.getString("fname");

Take a look at complete example here. Also here is a web based tool for creating/editing JSON.

1 Comment

Thanks for your suggestion :)

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.