I am getting gson error when I parse following json. If anybody have some idea how can i parse this json then please tell me.
{
"video": [
"",
"",
"",
{
"date": "25-sep-2014",
"day": "monday",
"city": "chandigarh"
}
] }
You can using gson library, it's easiest way Using Gson to parse json object
Related
I'm using GSON for parsing response from a Volley request and got stuck in creating a GSON format when the response has a property that can either be a string or an object or an array perhaps... e.g content
{
"data": {
"date_updated": "2016-12-21T03:55:29.955Z",
"date_created": "2016-12-21T03:55:29.955Z",
"content": "String here",
"content": {
"longitude": "",
"latitude": ""
},
"status": "PROC",
"_id": "5859fd31a93c7235575d62db"
}
}
My current process in creating a GSON model is:
Create a java class
Right click and select Generate > GSONFormat
Paste the object I'm trying to convert then use it in Volley.
I tried the above object but it doesn't proceed. I think it's because of same property name.
Thanks for your advice.
You can use GsonFormat, you can look this:
https://github.com/zzz40500/GsonFormat
My Json response is something as below and confused how to parse it using GSON.
Please have look on the following and guide me how i can parse it using GSON.
{
"GetMICSDataResult": {
"CONVERTIONFACT": [
{
"CONVERSIONFACT": "1",
"ITEMNO": "S1300W",
"UOM": "Ea."
},
{
"CONVERSIONFACT": "1",
"ITEMNO": "S1300Y",
"UOM": "Ea."
}
],
ITEMDETAILS": [
{
"ITEMDESC": "FluorescentDeskLamp",
"ITEMNO": "A11030",
"LOCATION": "1",
"PRICELIST": "WHS",
"QTYONHAND": 164,
"UNITPRICE": 38.3,
"UOM": "Ea."
},
{
"ITEMDESC": "FluorescentDeskLamp",
"ITEMNO": "A11030",
"LOCATION": "2",
"PRICELIST": "WHS",
"QTYONHAND": 247,
"UNITPRICE": 38.3,
"UOM": "Ea."
}
]
}
}
Gson gson = new Gson();
YourClass class = gson.fromJson(jsonInString, YourClass.class);
There is easy way to do that. Just use POJO generator http://www.jsonschema2pojo.org/ it will give you plain object with necessary annotations. also you can use Json formatter to validate your json https://jsonformatter.curiousconcept.com/ - the JSON you posted is invalid.
I Am getting this Json Response from server how to deseriallize the json string
{
"results": {
"status": "true",
"message": "Successfully Get Data",
"PdfData": {
"Pdf_Result": [{
"Pdf_File_Name": "aft.pdf",
"Subject_Name": "Maths",
"Subject_Id": "1",
"Chapter_ID": "M1111",
"Chapter_Name": "Relations and Functions",
"Downloaded": "YES"
}, {
"Pdf_File_Name": "date.pdf",
"Subject_Name": "Maths",
"Subject_Id": "1",
"Chapter_ID": "M1112",
"Chapter_Name": "Algebra",
"Downloaded": "YES"
}]
}
}
}
see jsonschema2pojo.org site and Gson library tutorial
https://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
This tutorials will teach you how to parse with Json using 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)
I have been trying to wrap my head around how to parse nested objects and arrays with GSON, still stuck. How can I parse the nested items listed in the "results" array?
{
"item": {
"results": [
{
"__metadata": {
"url": "google.com",
"type": "website"
},
"listed": true,
"market": 225,
"town": "Toronto"
},
{
"__metadata": {
"url": "twitter.com",
"type": "website"
},
"listed": true,
"market": 225,
"town": "Calgary"
}
]
}
}
How can I easily do this with GSON within Android?
Thank you!
Android Studio
Download Plugin "GsonFormat"
Create your model class
Open Code->Generate->Gson
Paste your json click ok - if json is valid then it will convert the following json to java class (pojo)
Now create Gson object
Gson gson=new Gson();
Convert Json to java object
T obj = gson.fromJson(contents, tClass);
Now use this object "obj" to get values