This question already has answers here:
Parsing JSON Object in Java [duplicate]
(5 answers)
Closed 5 years ago.
this is my json response
{
"status": true,
"code": 200,
"message": "Success",
"data": {
"id": "14"
}
}
how to read this values
You can use the help of Google's GSON library built for exact your pupose.
YourPojoClass model = new Gson().fromJson(jsonResponse, YourPojoClass.class);
Convert your json to PojoModel by using jsonschema2pojo.org
You can do it like this:
Create a JSONObject:
JSONObject jObject = new JSONObject(result);
To get a specific boolean
String aJsonString = jObject.getBoolean("status");
To get a specific String
String aJsonString = jObject.getString("message");
To get a specific Integer
int aJsonInteger = jObject.getInt("code");
Related
This question already has answers here:
Deserialization of generic collections with Gson
(2 answers)
Closed 4 years ago.
I have this JSON string:
[
{
"k":"number",
"v":"055 543 8679\n"
},
{
"k":"hours",
"v":"8:00AM - 9:00PM"
}
]
And I'm trying to parse it using Gson:
val customFields = Gson().fromJson(food.customFields, ArrayList<CustomField>()::class.java)
But somehow when I view it in breakpoint mode, it is parsed as a LinkedTreeMap
try thisval customFields = new Gson().fromJson(food.customFields, CustomField.class);
This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
{
"quote": "To understand the heart and mind of a person, look not at
what he has already achieved, but at what he aspires to.",
"author": "Kahlil Gibran (1883-1931)"
}
How to parse this with no any object name
There are different methods to parse json object
Simple way
JSONObject myJson = new JSONObject(yourJsonString);
String quote = myJson. getString("quote");
String author = myJson. getString("author");
Another and recomented way is parse json using Gson
For this you can refer below links
How to parse json parsing Using GSON in android
https://www.journaldev.com/2321/gson-example-tutorial-parse-json
How to do it ?
If you meet {} in your code , you can use JSONObject to parse it .
If you meet [] in your code , you can use JSONArray to parse it .
And if you meet [] in your code , you can use for loop to get value in it .
And you should use try catch in your code .
Try this .
try {
JSONObject jsonObject = new JSONObject(response);
String author = jsonObject.getString("author");
String quote = jsonObject.getString("quote");
} catch (JSONException e) {
e.printStackTrace();
}
This is absolutely normal json.
JSONObject json = new JSONObject(your_json);
String quote = json.getString("quote");
String author = json.getString("author");
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I get a response but it's different for every user.
I want to extract the UserID & Name. How can I do this?
The response String:
{
"sessionkey":"f4b54dedlfkjhgdlfkjghdfslkjgh1a255242bf71597f4b35ac882f0702",
"user" : {
"userID" : "1",
"name":"Test"
}
}
I want to save 1 = (String userid)
and save Test = (String name)
Thanks in advance
This is just a simple JSON blob, so use a JSON Parser to extract the data.
My lib of choice is org.json.json.
String blob = "{ origin string }";
// create a new json object from the main blob
JSONObject root = new JSONObject(blob);
// extract the session key
String sessionKey = root.getString("sessionkey");
// extract the user object
JSONObject user = root.getJSONObject("user");
// extract the user id
String userId = user.getString("userID");
{
"id": "1",
"name": "test1",
"data_city": "a:35: {i:22;s:6:\"61,800\";i:23;s:6:\"61,800\";i:24;s:6:\"61,800\";i:25;s:6:\"61,800\" ;i:26;s:6:\"61,800\";i:27;s:6:\"61,800\";i:28;s:6:\"61,800\";i:29;s:6:\"61,800\";i:30;s:6:\"61,800\";i:31;s:6:\"61,800\";i:54;s:6:\"61,800\";i:16;s:6:\"61,800\";i:32;s:6:\"61,800\";i:52;s:6:\"61,800\";i:21;s:6:\"61,800\";i:33;s:6:\"61,800\";i:37;s:6:\"61,800\";i:34;s:6:\"61,800\";i:36;s:6:\"61,800\";i:38;s:6:\"61,800\";i:41;s:6:\"61,800\";i:35;s:6:\"61,800\";i:39;s:6:\"61,800\";i:40;s:6:\"61,800\";i:42;s:6:\"61,800\";i:44;s:6:\"61,800\";i:43;s:6:\"61,800\";i:46;s:6:\"61,800\";i:45;s:6:\"61,800\";i:47;s:6:\"61,800\";i:49;s:6:\"61,800\";i:53;s:6:\"61,800\";i:50;s:6:\"61,800\";i:48;s:6:\"61,800\";i:51;s:6:\"61,800\";}"
}
This is my json response
i need to get data from string data_city
First of all, your JSON is not valid - probably a copy paste error.
Anyway, there are several ways to do this. One option is to use the built in JSONObject type:
String jsonString = "YOUR_JSON_STRING";
JSONObject objJSON = new JSONObject(jsonString);
String dataCity = objJSON.getString("data_city");
Hope this helps. Be sure to run your JSON String through a validation tool first. This one works nicely: https://jsonformatter.curiousconcept.com/
This question already has answers here:
Sending and Parsing JSON Objects in Android [closed]
(11 answers)
Closed 7 years ago.
{
"data:"
{
"calendar":
[
{
"Booked":["13-11-2015","12-12-2015"],
"Available":["20-12-2015","22-12-2015"]
}
]
}
}
Could any one please help me how to get date from json array string
Your json should be like this
{
"data": {
"calendar": [
{
"Booked": [
"13-11-2015",
"12-12-2015"
],
"Available": [
"20-12-2015",
"22-12-2015"
]
}
]
}
}
You can use json online parser to check the validity of a json.
Now, coming back to your actual question.I dont have an android IDE so I will post a quick code to give u an idea
JSONObject jparentobj= new JSONObect("jsonString");
JSONObject datajObj= jparentobj.getJSONObect("data");
JSONArray calenderarray = datajObj.getJSONArray{"calender");
for (int i = 0; i < calenderarray.size(0); i++){
String booked = calenderarray.get(i).getString("Booked");
//Like this get each entity
}
Hope this helps you..