DeSerializing dynamic array in GSON - android

I am working on JSON response . I want to store JSON response in model then store the model values into DB.
So that I used GSON to parse the JSON response.
In my response I have some dynamic JSON array names. The sample and part of the response is below.
{
"TD": [
{
"TimeClockDetails": {
"March 2, 2016": [
{
"TimeclockId": "148148",
},
]
}
In that response , I have the ""March 2, 2016":" array . If a static array name means I can use #SerializeName("arrayname_in_response").
Now, how can I deserialize this dynamic array name ?
If need more clarification,I am ready to provide.

One solution would be to use a Map.
#SerializedName("TimeClockDetails")
private Map<String, TimeDetail> mTimeClockDetails;
This way the date keys would end as keys in the map.

Related

How to parse the different type of json

When I call an api with specific parameter, I get a json like this:
{
“name”:“my name”,
“family”:”my family”
},
{
“name”:“my name1”,
“family”:”my family1”
}
But when I call same api with different parameter I get same JSON but with the different type for example an empty array :
[]
How can create a data model for this type of response in kotlin?

How to parse a JSON which contains an array with only square brackets using Gson?

This is my JSON response:
{
"email":[
"This field must be unique."
]
}
I want to retrieve the value of json object email and display it. I tried using Gson but always getting null. Created a model class with email variable with type JSONArray -- still no luck.
Thanks in advance .
Note that the email attribute is actually an array of strings.
The object to be deserialized by Gson should look like the following:
public class Response {
#SerializedName("email")
public List<String> emails;
}
Then using gson
Response response = gson.fromJson(json, Response.class);
And then you can access to that object by doing
response.emails.get(0)

Retrofit parse a json with dynamic keys

I want to use retrofit for my webservices.
I am getting a problem how can I define an object with dynamic Keys.
I am trying to get data for today and tomorrow. Dates will always change.
Here is the json:
{ "2015-11-13": [ ], "2015-11-14": [ ]}
If you are using GSON I guess you can declare the response as JsonObject in your call like this :
#GET("your_api_path")
Call<JsonObject> getDateData();
And then parse it when you get the response
Type mapType = new TypeToken<Map<String, List<YourModel>> >() {}.getType();
Map<String, List<YourModel>> result= gson.fromJson(jsonResponse, mapType);

How to parse such type of JSON data for the dynamic key

{"Freedom Plan 4900":
{
"sale_price1":["$10.00","$20.00","$25.00","$50.00","$100.00"],
"loc_rate1":["India (2.0¢ per min)"],
"net_minutes1":["500","1,000","1,250","2,500","5,000"],
"loc_rate":["India - Mobile (1.5¢ per min)"],
"net_minutes":["666","1,333","1,666","3,433","6,866"]
},
"Super Offer Plan":
{
"sale_price1":["$10.00"],
"loc_rate1":["India (1.5¢ per min)"],
"net_minutes1":["667"],
"loc_rate":["India - Mobile (1.0¢ per min)"],
"net_minutes":["1,000"]
}
}
i want to parse this json string
I would use something like GSON:
http://sites.google.com/site/gson/gson-user-guide
Create classes to hold the data (e.g. PhonePlan etc) and let GSON parse the JSON string.
PhonePlan plan = gson.fromJson(json, PhonePlan.class);

How to get a JSON with dollar sign

suppose I have {"data": {"243232": {"id": "testid","name": "test" } }}
so how to get correct value thanks.
you cant directly pass above String with Gson as "243232" is number and we cant declare variable which start from number, So for parsing this you must modify the string with some notification
i.e. {"data": {"temp243232": {"id": "testid","name": "test" } }}
here i modified string manually "243232" by "temp243232", now you can parse it
You can use a JSON parser for Java like Google GSON. This works best if the class whose object your data in json represents, is known to you.
String str = ""; //contains your json string
Gson gson = new Gson();
MyClass var = gson.fromJson(str,MyClass.class);
where MyClass will be a class which represents the object.
Then you can access data just like you'd do from a data structure in your program, it'll be something like var.data.

Categories

Resources