Parse JSON Array in Response using Retrofit Android - android

Parse the below JSON Response using Retrofit.
Thanks in advance.
[
{"borrowerId":6,
"borrowerName":"archanaB",
"loanId":"LN3",
"numbersOfEmisPaid":2,
"profit":300.0,
"disburmentAmount":5000.0,
"emisReceived":1967.0
},
{
"borrowerId":6,
"borrowerName":"archanaB",
"loanId":"LN14",
"numbersOfEmisPaid":1,
"profit":150.0,
"disburmentAmount":5000.0,
"emisReceived":983.0
},{
"borrowerId":2,
"borrowerName":"NarendraB",
"loanId":"LN12",
"numbersOfEmisPaid":6,
"profit":175.0,
"disburmentAmount":35000.0,
"emisReceived":36050.0
},
{
"borrowerId":6,
"borrowerName":"archanaB",
"loanId":"LN4",
"numbersOfEmisPaid":18,
"profit":133.0,
"disburmentAmount":5000.0,
"emisReceived":344.0
},
]

You can use one of the Retrofit2 Converter.Factory -
Gson: How to get the value with GSON / Retrofit? [duplicate]
Jackson: Retrofit and Jackson and parsing JSON
kotlinx-serialization: Kotlin Serialization Converter

Convert json to pojo using this site if Java
http://www.jsonschema2pojo.org/
if using kotlin
https://www.json2kotlin.com/
and use it like that, after implementing you will get Object so no need to parse
#POST(your_api)
fun details(
#QueryName id: Int
):
Observable<yout_model_generated_from_above_link>

Related

kotlin, android, json serialization

i'm using google gson converter to serialize an json response from an api call into an kotlin object,
one of the json field is a json object
so this didn'T work:
#Expose
#SerializedName("properties")
val properties: List<ThingProperty>,
i got the following error:
Api call failed: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1
but this work fine:
#Expose
#SerializedName("properties")
val properties: JsonObject,
but i then received a JsonObject and not a list of "second level" kotlin object
So the question is, using serialization or something like that,
how can i just received the original json and convert it properly, for this field, into a list of object based on the jsonObject
note, i received my data using retrofit2
Edit:
there is a json example for this item:
"properties":{
"ActiveEvent":{
"name":"ActiveEvent",
"value":false,
"visible":true,
"title":"Active Event",
"type":"boolean",
"#type":"BooleanProperty",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/ActiveEvent"
}]
},
"PreHeatEvent":{
"name":"PreHeatEvent",
"value":false,
"visible":true,
"title":"Pre-Heat Event",
"type":"boolean",
"#type":"BooleanProperty",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/PreHeatEvent"
}]
},
"PostHeatEvent":{
"name":"PostHeatEvent",
"value":false,
"visible":true,
"title":"Post-Heat Event",
"type":"boolean",
"#type":"BooleanProperty",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/PostHeatEvent"
}]
},
"NextEvent":{
"name":"NextEvent",
"value":null,
"visible":true,
"title":"Next Event",
"type":"string",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/NextEvent"
}]
},

How can I take array out of double quotes

Suppose if I've a string as
"[{"key1": value1}, {"key2": value2}, and so on...]"
is there a way to convert it into array:
[{"key1": value1}, {"key2": value2}, and so on...]
Actually I've to post a JSON request in my android app which should be in the following format:
{
"user_id": "5665",
"restaurant_id": "5",
"total_cost": "660",
"food":[
{"food_item_id": "50"},
{"food_item_id": "51"}
]
}
As in the above code, I've to use the array for food key which is stuck in string which I get using the following code:
val gson = Gson()
val menuIds = gson.toJson(listOfMenuIds)
You can create a dataclass for food_items to represent a item in the "food" array.
Then create another dataclass which has "user", "restaurant_id", ... so on... as members, including a member var foods: List
Then pass the second class to the Gson converter

Send a Json which is like key:{key:value,key:[]} with Volley

I am new to using Volley and parsing complex Json.
I have Json like:
"timings": {
"summary": "Mon-Fri 9am-6pm",
"detail": [
"Mon 9am-6pm",
"Tue 9am-6pm",
"Wed 9am-6pm",
"Thu 9am-6pm",
"Fri 9am-6pm"
]
}
I am not sure how to send it. I have tried to search for solutions but no luck.
Some help will be really helpful.
I suggest to use Gson library.
And with Gson create class with name of Timings and contain 2 variables .
data class Timings ( val summery: String , val details : List<String>)
val gson: Gson()
time = gson.fromJson(jsonString,Timings::class.java)
Other way is use Json Object and json array to parse it manually

Broken server response handling with Moshi

The expected json response from server should be :
{
"teacher": {
"123": {
"_id": "389",
"name": "test_fast_teacher1"
}
}
}
Server returned json with this :
{
"teacher": [
]
}
Anyway to handle this broken json response?
Before I switching from Gson, the teacher object will still be deserialised, just that it will be null. By using Moshi, the error would be threw and I can't proceed with the other json which is serialised correctly.
Please refer to the link for the reply from author.
How about something like this?
Moshi moshi = new Moshi.Builder()
.add(DefaultOnDataMismatchAdapter.newFactory(Teacher.class, null))
.build();
JsonAdapter<Teacher> adapter = moshi.adapter(Teacher.class);
Teacher teacher = adapter.fromJson(json);
// teacher == null
where DefaultOnDataMismatchAdapter is Jesse's code you can copy into your code base.
When the Teacher type comes back in an unexpected format that would produce a JsonDataException, it will default back to your set value (in this case, null).

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

Categories

Resources