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);
Related
I want to send this parameter in request and tried to send a simple array list but that was not working
"Language": [
"string","string","string"
]
If "Language" is key and ["string1","string2","string3"] is value then create method in interface as below.
#FormUrlEncoded
#POST(LINK_API)
Call<ResponseModel> getResponse(#Field("Language") String languageArray);
and call it using interface instance like this :
JSONArray languageArray = new JSONArray();
languageArray.add("string1");
languageArray.add("string2");
languageArray.add("string3");
String langArray = languageArray.toString();
Call<ResponseModel> responseModel = apiObject.getResponse(langArray);
responseModel.enqueue(...);
This will work perfectly.
try post query with field parameter as string..
convert your jsonObject to String using,
String b = json_object.toString();
At Server Side, convert string back to JsonObject or whatever else you need.
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)
I have a JSON Like this
{ "video":{
"video_3745":{ },
"video_3437":{ },
"video_3471":{ },
"video_4114":{ }
}
}
In which every "video_xxxx" is of the SAME type. Is there a way to treat the "video" field as an array of that type? I need to iterate over all the videos, but the API is not sending them in an array, and I don't know how to model a class to receive this JSON without having to manually specify all the field names...
does GSON or LoganSquare have anything to help me out with this?
Try something like this
JSONObject video= json.getJSONObject("video"); // json is the whole response
Iterator x = video.keys();
JSONArray jsonArray = new JSONArray();
while (x.hasNext()){
String key = (String) x.next();
jsonArray.put(video.get(key));
}
You can't treat them as an array, but with the org.json.JSONObject class you can get the list of keys and iterate over them. I believe in GSON the JsonObject.entrySet method will allow something similar.
https://developer.android.com/reference/org/json/JSONObject.html
https://google.github.io/gson/apidocs/com/google/gson/JsonObject.html
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.
i would like to deserialize string
{
"foo":5,
"bar":"03.10.01"
}
I want to deserialize it to Map which will contain only Strings.
Using sample code gives me Integer in place of "5" and java.Util.Date instead of date String.
Map<Object, Object> map = new HashMap<Object, Object>();
map = (Map<Object, Object>) gson.fromJson(new InputStreamReader(inputStream), map.getClass());
I don't know what will be in JSON, that's why i'm parsing it into Map
#reactivemobile
It's true but what if JSON is like:
As i said, I don't know what will be in JSON, it can look like:
{
"array": [
{
"eggs": 5,
"bb": "03.10.01"
}
],
"foo": 5,
"bar": "03.10.01"
}
Giving this with Map will create Map that contains ArrayList, foo, bar. In ArrayList there will be LinkedTreeMap that will contain "eggs" and "bb".
So your answer is correct, but not in the case where JSON can have dynamic structure.
Use
Map<String, String> map
To force the values to be Strings