Android How to Deserialize the Json String Using Pojo Class - android

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

Related

JSON data - How to parse?

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

How to create POJO class for retrofit with dynamic keys names

I am sorry but i couldn't even phrase the question well but here it goes.
I am getting a JSON response like this:
{
"results": {
"b2bc01": [{
"message": "Successfully created",
"_id": "596c8b25ce2350e41600002f",
"status": "Success",
"code": 200
}],
"b2bc02": [{
"message": "Successfully created",
"_id": "596c8b25ce2350e416000030",
"status": "Success",
"code": 200
}]
.
.
.
"b2bc0n":[{
"message": "Successfully created",
"_id": "596c8b25ce2350e416000030",
"status": "Success",
"code": 200
}]
}
}
How do i create POJO class for this type of JSON. I tried in jsonschema2pojo but i feel its not a good result.
Please help. Thank in advance
Your POJO file
class MyPojo {
HashMap<String, ArrayList<MyModel>> results;
class MyModel {
String message;
String _id;
String status;
int code;
}
You can only use pojo with this data:
[{
"message": "Successfully created",
"_id": "596c8b25ce2350e41600002f",
"status": "Success",
"code": 200
}]
And if you use retrofit 2 and gson, I recommend you use interface JsonDeserializer
with .registerTypeAdapter()
Example
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(GpcProductDetail.class, new GpcProductDeserializer())
.create();
And parse manually that part of json.

How to parse JSON using GSON

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.

JSON parsing of array that contain object and string both

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

Gson ignore json field and deserialize

I'm working with android and gson and i was wondering if i can ignore a field from the webservice.
this its my gson object
Gson gson = new GsonBuilder().serializeNulls().create();
So what i need is to ignore validator field and deserialize to my custom bean in the following code.
Its any way to do that with gson ?
{ "posts":
[
{
"username": "John",
"message": "I'm back",
"time": "2010-5-6 7:00:34"
"validator":[{
"prog": "Name1",
"name": "Name2",
"computername": "Name3",
"date": "2010-11-20 19:39:55"
}]
}
,
{
"username": "Smith",
"message": "I've been waiting",
"time": "2010-4-6 10:30:26"
"validator":[{
"prog": "Name1",
"name": "Name2",
"computername": "Name3",
"date": "2010-11-20 19:39:55"
}]
}
]}
Thanks in advance
Cheers
You can call gson.fromJson(inputString, localObject) and if localObject doesn't declare a validator variable, it will just skip it.

Categories

Resources