How to POST following data using retrofit - android

I want to post the following JSON object in a POST request over retrofit
{
"Inputs": {
"input1": {
"ColumnNames": [
"Name",
"Weekday",
"Time",
"Type"
],
"Values": [
[
" ",
"1",
"9:00:34",
"OUTGOING"
],
]
}}
How do I represent this as a GSON object? I have only found very easy examples online (like {'Foo':'bar'} sort). Any help is greatly appreciated

Use http://www.jsonschema2pojo.org/ to generate the java classes

{
"Inputs": {
"input1": {
"ColumnNames": [
"Name",
"Weekday",
"Time",
"Type"
],
"Values": [
[
" ",
"1",
"9:00:34",
"OUTGOING"
]
]
}
}
}
It looks like you have an "input" object, containing a string array (ColumnNames) and a two dimensional string array (Values). You could interpret it as a java model object like the following
class Input {
String[] ColumnNames;
Value[][] Values;
}
You can then use a json library like Gson to convert your json to and from this model object, eg new Gson().fromJson(jsonInput, Input.class).
The json you actually have shown us wraps your Input object in two other objects though, so make sure to handle them appropriately.

Related

kotlinx.serialization - partial deserialization

I have a very large json file within my android app with the following struture:
[
{
"id": 123,
"data": {
...
}
},
{
"id": 456,
"data": {
...
}
},
...
]
Depending on the item selected in the UI, I need to deserialize only the item with the corresponding id, but right now I'm deserializing the whole file and then do the filter, which takes time and memory to just extract the id I need.
Is there a way to deserialize only the section I need?
I'm using kotlinx.serialization.

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"
},
]
}
]
}

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)

How to parse multiple nested JSON objects/arrays with GSON?

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

GSON mapping without entity name

I need to map a JSON object to a class using GSON, here is the JSON object:
{
"protocols": [
[ "https", 39 ],
[ "http", 1 ]
],
...
}
Generally if there are entity names specified it is easy to do something like this:
{
"protocols": [
[ "name":"https", "count":39 ],
[ "name":"http", "count":1 ]
],
...
}
class ProtocolItem {
#SerializedName("name")
String protocolName;
#SerializedName("count")
int count;
}
However since no entity names are specified in this case, I am not sure how to do the mapping for this. Please point some directions for me if you are familiar with the case.
Thanks
Unlike your first example,
[ "name":"https", "count":39 ],
is invalid JSON as you can either specify an array using [1, 2] without any names or a map using {"x": 1, "y": 2} with "entity names" as keys. So the solution is simple:
{
"protocols": [
[ "https", 39 ],
[ "http", 1 ]
],
}
is a map with a single key and a value which is an array of arrays of objects. You can map it as
class All {
Object[][] protocols;
}
You must use Object here, as it needs to accept both strings and ints. Instead of arrays, you can use Lists.
I guess, you'd prefer to serialize it as
class All {
Map<String, Integer> protocols;
}
This is possible, too, but you need a TypeAdapter. The very first linked example shows clearly how to do it (start with beginArray, in a loop test JsonToken.BEGIN_ARRAY and do nextString and nextInt, etc.).

Categories

Resources