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"
}]
},
Related
I have this json response from the api, and the response can't be change
data class Weather (
val category: String,
val id: String,
val meta: Meta
)
data class Meta (
val id: String,
val name: String,
val details: String
)
Json respose
{
"weather" : {
"category": "articles",
"id": "1",
"meta": {
"id": "1",
"name": "The shortest article. Ever.",
"details": "see"
},
"weather" : {
"category": "articles",
"id": "2",
"meta": []
}
If meta is empty, it come with an array but if not empty, it come with object.
Retrofit throws
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
the api can't be modify so this has to be fix on client end. How can I solve this
You can not make this possible
meta object must be an object when have value and null when do not have any value, or an array with value when exist and empty when not exist.
meta can not be an array and object in the same time.
this is very bad mistake from who created this response body.
You can use Any type for meta. and put check at your code level like this.
data class Weather (
val category: String,
val id: String,
val meta: Any
)
if(meta is Meta)
parse it to your Meta object
else
parse it to list
How To Parse Response of Multiple Types?
Key is like (suppose student_list is a key of list types when student_list is empty then it makes as a string like student_list=""), How to manage this types of response using Retrofit? I am using MVVM Model with retrofit.
My Response:
when I get Data into the List
{
"status": 200,
"data": [
{
"prod_month_total": 2989.61,
"product": "GAS"
},
{
"prod_month_total": 39566.22,
"product": "OIL"
},
{
"prod_month_total": 83912.55,
"product": "OTHER"
}
]
}
when List is Empty Then Response:
{"status":404,"data":"No result found"}
I am getting this Error:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 23 path $.data
first create the right model calss use this site http://www.jsonschema2pojo.org
than use
`if( reponce.isSucessful){
if(responce.status==200){
//code here
}
else{
// find the error
}
}else
{
//
}`
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>
I am trying to write a GraphQL mutation which contains a JSON parameter.
signup( firstName: String! lastName: String tel: String location: JSON email: String! gender: Gender nic: String! userType: UserType isActive: Boolean password: String! )
I Create a JSON object and parse it into sever.
val location = JSONObject()
location.put("latitude","1.232")
location.put("longitude","1.34234")
But it gives errors
Reason: 'location' Not valid JSON (line 1, column 11):\nmutation ($data: UserCreateInput!)
All other parameters(String and enum) are correct. I Checked it.
What I am guessing is going on is this:
Someone on your team has created a custom Scalar JSON with a corresponding coercing. Your GraphQL request variable location should most likely be a string that that gets parsed to json at your server level.
The fact that the error says "location" is not a valid JSON means that your actual GraphQL request variable is coming through like this:
{
"variables": "{"location": "\"location\": {\"latitude\": 12.34, \"longitude\": 12.34}",
...rest
}
GraphQL will pass a value "\"location\": {\"latitude\": 12.34, \"longitude\": 12.34}" to your custom json parser, which is not a valid json
I'm using GSON for parsing response from a Volley request and got stuck in creating a GSON format when the response has a property that can either be a string or an object or an array perhaps... e.g content
{
"data": {
"date_updated": "2016-12-21T03:55:29.955Z",
"date_created": "2016-12-21T03:55:29.955Z",
"content": "String here",
"content": {
"longitude": "",
"latitude": ""
},
"status": "PROC",
"_id": "5859fd31a93c7235575d62db"
}
}
My current process in creating a GSON model is:
Create a java class
Right click and select Generate > GSONFormat
Paste the object I'm trying to convert then use it in Volley.
I tried the above object but it doesn't proceed. I think it's because of same property name.
Thanks for your advice.
You can use GsonFormat, you can look this:
https://github.com/zzz40500/GsonFormat