JSON data - How to parse? - android

Suppose I have data in JSON. How can I parse the data so I can get the values of nested objects (e.g. kind, id and title)?
My JSON data is:
{
"kind": "youtube#playlistItemListResponse",
"items": [
{
"id": "UExkc1JWcGJNV19LSH",
"items":[
{
"title": "New Updates",
}
],
}],
}

Well you should remove all the commas since last items should not be followed by one.
So more like:
{
"kind": "youtube#playlistItemListResponse",
"items": [{
"id": "UExkc1JWcGJNV19LSH",
"items": [{
"title": "New Updates"
}]
}]
}
Other than that you can just use JSONObject lib and extract the value you need. For example to get the kind:
JSONObject jsonObj = new JSONObject(strInput);
String kind = jsonObj.getString("kind"); // kind now contains "youtube#playlistItemListResponse"
where strInput is the above json as a string.
More info: https://developer.android.com/reference/org/json/JSONObject

Related

how to write PODO from JSON that has 2 object list withe same name but different values in flutter?

I am trying to fetch JSON Object which has 2 list object with the same name, but has a different value. The object list named "items", the first "items" has "products_title,product_image,link", and the second "items" has "article_title, article_image,link". How to write PODO ?
I've been trying to writes PODO, but even i try to change the model, it still does not work. I try the other REST API, for the example "https://jsonplaceholder.typicode.com/pothos" its working fine. But if i try used my JSON its getting error, i wonder how to write PODO ?
this is the JSON i am using :
{
"data": [
{
"section": "electronics",
"items": [
{
"product_name": "Cellphone",
"product_image": "cellphoneImage.png",
"link": "https://cellphone.html"
},
]
},
{
"section": "facts",
"section_title": "Title section",
"items": [
{
"article_title": "Facts",
"article_image": "https://www.facts.png",
"link": "https://www.facts.html"
},
]
}
]
}

How parse JSON with "key is value" style JSONObject to normal with GSON [duplicate]

This question already has answers here:
Array of JSON Object to Java POJO
(3 answers)
Closed 3 years ago.
I have this JSON file
"Items": [
{
"Name": "Id",
"Value": "102"
},
{
"Name": "TypeUid",
"Value": "333"
}, {
"Data": {
"Items": [
{
"Name": "Id",
"Value": "106"
},
{
"Name": "TypeUid",
"Value": "444"
},
{
"Name": "Uid",
"Value": "1322"
},
{
"Name": "Name",
"Value": "Alex"
}
]
In this file key-value pairs are kept inside JSONObject. How i can parse this file with GSON like this?:
"Items": {
"id" = 102,
"typeId" = 333,
"name" = "Alex"}
I don't understand how i can get normal POJO objects for GSON with this kind of JSON file.
You can simply create a model class to store the data like
class Items{
String/int id;
String/int typeId;
.
.
}
Then you can make a list which will hold the data like
List<Items> items = new ArrayList<>();
Then read the json string and parse it to form the list containing the data.
Refer this answer to convert json to list: Link

Nested JSON parsing in android using url

I am new to JSON parsing and I am stuck in this nested JSON parsing.Here is the attached JSON data for one city only which I get from the url :
{
"CityCode": 142,
"CityName": "Yavatmal",
"State": {
"StateCode": 21,
"StateName": "Maharashtra",
"CountryCode": 1,
"Country": {
"CountryCode": 1,
"CountryName": "India",
"IsdCode": "+91"
}
},
"Country": {
"CountryCode": 1,
"CountryName": "India",
"IsdCode": "+91"
},
"GPlaceId": "ChIJ3U60i8zo0zsR4L7sQeqtd0g",
"Latitude": 20.388794,
"Longitude": 78.120407,
"ActiveStatus": 1
}
Can anyone tell me how to parse this type of JSON data in such a way that I can display each key in a textView.
the scope sorrounded by "{" "}" are called json object. You can extract like `
String jsonData = ""//received json string
jsonObject obj = new JSONSObject(jsonData);`
obj.getString("CityCode");
obj.getJSONObject("state");
if the key hold the single value , then you can get that in string. If the key itself hold another jsonobject , extract that into separate jsonobject and repeat.

Deserializing different objects in JSON array (GSON)

The JSON result for getUsers I get from the server looks like this:
{
"result": [
{
"meta": {
"rows": "3"
}
},
{
"items": [
{
"id": "1",
"name": "Steve",
"age": "30"
},
{
"id": "2",
"name": "Mary",
"age": "29"
},
{
"id": "3",
"name": "Bill",
"age": "58"
}
]
}
]
}
How can I deserialize it by GSON in my android app (I'm using retrofit)?
I can't imagine any wrapper classes because of the different object types in result.
Any help would be appreciated!
For good example
Converting JSON to Java
Other way, you can convert your json to a java object
Please use org.json library http://www.json.org/java/index.html
Then, for example
json = new JSONObject(YOUR_JSON).getJSONObject("result");
JSONArray items = data.getJSONArray("items");
String name = items.getJSONObject(0).getString("name");
You can write a TypeAdapter for a type that will be (de)serialized to(from) array. You can even make it generic, so it will work with type like Pair<A, B>. Here is an example for non-generic type: https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java — it (de)serializes Entry to(from array).
Disclaimer — I have not written nor tested that code, but it seems legit.
If you only encounter such problem once (like in your example), you may not bother making it generic, just write TypeAdapter for your specific pair of 2 different classes. The reading code is quite straightforward:
in.beginArray();
SomeClass1 info1 = gson.getAdapter(SomeClass1.class).read(in);
SomeClass2 info2 = gson.getAdapter(SomeClass2.class).read(in);
in.endArray();
return new SomeContainerClass(info1, info2);
(see https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java#L52)

Parsing JSON in android to add markers in Google Map

I have an url,get and response given. I have to parse the JSON and show the locations in Map in android. I dont need you to code. I am new in JSON. please tell me some steps how i can do that. how should i start and which steps i should take?
Url: "Some url"
Sending data:
{
"tag": "getAvailableDriver",
"lat": 41.022348,
"lng": -91.966721
}
RESPONSE:
{
"posts": [
{
"success": "1",
"driver": [
{
"id": "768",
"lat": "41.022848",
"lon": "-91.966884",
"recorded_datetime": "2014-07-20 20:18:03",
"user_id": "403",
"cabbi_state": "OnJob",
"vehicleType": "Black Cab",
"driver_name": "black cab driver",
"pic_name": "userimage/403.jpg",
"rating": "0",
"car_model": "this",
"number_sit": "4",
"distance": 0.035574527536789
}
],
"operator": [],
"nearestdistance": [
{
"distance": 0.03541809,
"time": 5
}
],
"car_models": [
"Taxi"
]
}
]
}
I just want anyone of you to guide me or show me the way. And also please someone tell me what will be the use of "send data" here. What is the use? I promise i will not ask any basic questions after i catch the grip. Help me please.
You can save the parsed data in an array, and for what I can see, there's a lot of tags in the JSON object. I suggest you to create an object of the type "Driver" or "Car", and then save all the data in that object. Once you have the data parsed and saved in an Array of the type of your object (Let's say an array of "Drivers" then you can use the info to add it into the Map.

Categories

Resources