Reputation: 123
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.
Upvotes: 0
Views: 138
Reputation: 459
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
.
Upvotes: 0
Reputation: 81
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:
String uname = json.getJSONObject("data").getString("fname");
Upvotes: 0
Reputation: 37506
String uname = json.getJSONObject("data").getString("fname");
Upvotes: 1
Reputation: 41935
You have to first get the data
property and then get the fname
property. fname
is inside the data
property.
Upvotes: 0
Reputation: 15042
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");
Upvotes: 5