Retrofit doesn't recognize nested json response - android

I'm using Retrofit to get json response. I also generated model classes with http://www.jsonschema2pojo.org/ However it doesn't work. The problem is json response doesn't fit to my java object. Am I miss something?
{
"status":"ok",
"status_message":"Query was successful",
"data":{
"movie_count":1,
"limit":20,
"page_number":1,
"movies":[
{
"id":4281,
"url":"https:\/\/yts.to\/movie\/the-third-man-1949",
"imdb_code":"tt0041959",
"title":"The Third Man",
"title_long":"The Third Man (1949)",
"slug":"the-third-man-1949",
"year":1949,
"rating":8.3,
"runtime":93,
"genres":[
"Film-Noir",
"Mystery"
],
"language":"English",
"mpa_rating":"Not Rated",
"background_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/the_third_man_1949\/background.jpg",
"small_cover_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/the_third_man_1949\/small-cover.jpg",
"medium_cover_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/the_third_man_1949\/medium-cover.jpg",
"state":"ok",
"date_uploaded":"2015-07-13 10:22:11",
"date_uploaded_unix":1436739731
},
]
},
"#meta":{
"server_time":1436799207,
"server_timezone":"Pacific\/Auckland",
"api_version":2,
"execution_time":"34.67 ms"
}
}

There's a problem at line number 31 where you put a comma(,) after the 2nd bracket { . Remove the comma and everything is fine .

Related

Parsing complex JSON using GSON

I was wondering what would be the most effective way to parse this JSON
[
{
"name":"heading-1",
"value":"Heading",
"type":"heading",
"css":"",
"helpText":"",
"size":"Small",
"displayLogic":{
"type":"show",
"rules":{
}
},
"guid":"fb39e01c-2802-461f-984a-b76aeb6bed33"
},
{
"name":"text-1",
"label":"Equipment name",
"value":"",
"placeholder":"Enter Text here ...",
"type":"text",
"css":"",
"helpText":"",
"validations":[
{
"type":"required",
"details":{
"active":false,
"message":"Required!",
"values":{
}
}
},
{
"type":"minlength",
"details":{
"active":false,
"message":"Please enter more text!",
"values":{
"min":"3"
}
}
},
{
"type":"maxlength",
"details":{
"active":false,
"message":"Your response was too long!",
"values":{
"max":"7"
}
}
}
],
"dataGridColumnInfo":{
"position":"1",
"heading":"New Text Box",
"displayType":"text",
"actionType":"view"
},
"displayLogic":{
"type":"show",
"rules":{
}
},
"guid":"8a58826e-7957-4f2c-8ea7-acebf624ac25"
}
]
This is just a snippet of my JSON, as the whole thing is pretty long. As you can tell though, it gets a little complicated. I'm using GSON to parse, and I am not sure if I should have a class for each object, or if I should just do it manually. I also need to be able to edit the json and write it to a file. Any suggestions?
use http://www.jsonschema2pojo.org. It will parse your json and create all the required classes

gson parse object when there is data and array when no data

I use retrofit2 + gson.
My question is about when server has data it send me json like:
{
"next":false,
"total":4,
"start":0,
"rows":[
{
"calls":29,
"new_calls":29,
"new_quality_calls":10,
"item":"MzY4NzA3NDA0Mw",
"sessions":3,
"calls_per":966.66666666666663
},
{
"calls":15,
"new_calls":15,
"new_quality_calls":5,
"item":"test312312312",
"sessions":2,
"calls_per":750
},
{
"calls":0,
"new_calls":0,
"new_quality_calls":0,
"item":"test",
"sessions":4,
"calls_per":0
},
{
"calls":0,
"new_calls":0,
"new_quality_calls":0,
"item":"test2",
"sessions":2,
"calls_per":0
}
],
"summary":{
"sessions":11,
"calls":44,
"new_calls":44,
"new_quality_calls":15,
"calls_per":400
}
}
But when there is no data, it send me:
{"next":false,"total":0,"start":0,"rows":[],"summary":[]}
The problem is that summary is an object or array is some case.
i use this site to make gson calsses http://www.jsonschema2pojo.org/
When i got data its ok, but when there is no data i got an error:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 56 path
How to solve it automatically by retroft2 (.addConverterFactory(GsonConverterFactory.create())) without manual data parsing?
Add your own converter which will parse AST (readTree), check $.summary type, fix it, and then give this fixed AST to TypeAdapter.

what is the equivalent to newsoft json in android

i have problems i am searching and cant understand what type of json parser i can use for example i have local json and i want to prase it is there any sdk i can use near the newsoft json or ez to learn because i am windows phone and 8.1 developer and i sucks with android and i really need it for graduation project i created almost every thing
[
{
"id": 1,
"time": 40,
"srcLong": 35.909124,
"srcLat": 31.973628,
"destLong": 35.898258,
"destLat": 31.985622,
"subSites": [
{
"id": 1,
"name": "location 1"
},
{
"id": 2,
"name": "location 2"
},
{
"id": 3,
"name": "location 3"
}
]
}]
plz how to get the response for example the code in newsoft json is like this
var serviceUri = "http://localhost:24728/api/sites";
HttpClient client = new HttpClient();
var response = await client.GetAsync(serviceUri);
var datafile = await response.Content.ReadAsStringAsync();
RootObject data = JsonConvert.DeserializeObject<RootObject (((string)datafile).Substring(1, ((string)datafile).Length - 2));
and of course you create the class to deal with object plz can some one help me with this and i need to remove the [] to be json object i just need the sdk i should use and where to start learn and if there no problem to tell me how to substring it and to put this code
string uri = "http://localhost:24728/api/sites/1";
HttpResponseMessage response2 = await client.PutAsJsonAsync(uri, data);
plz is there any equivalent in android.
Regards,
Ayesh.
Well from what you are saying it looks like you are unsure of how to get a response from the site, maybe you do not understand how to access the url initially. Or potentially that once you get the response and it is prehaps a string you do not know how to convert it into JSON to extract the information. Send information from android application to a Web Service and back. This link has a basic version of connecting to a url. And when you have a string, you can use JSONObject and JSONArrays to grab the information on that string, you can do JSONArray(string).

How to parse nested JSON array in android

How to get latitude and longitude object. please help
{
"category":[
{
"name":"Judging_Point",
"title":"",
"iconimg":"33.png",
"count":0,
"content":""
},
{
"name":"Street_Trading_Stalls",
"title":"Street Trading Stalls",
"iconimg":"34.png",
"count":0,
"content":""
},
{
"name":"static_sound_systems",
"title":"Sound Systems",
"count":29,
"iconimg":"24.png",
"content":[
{
"latitude":"51.52603767293402",
"longitude":" -0.2136941301305184",
"title":"4 Play",
},
{
"latitude":"51.52603767293402",
"longitude":" -0.2136941301305184",
"title":"4 Play",
},
One option would be to use JSONSimple with is a Java JSON lib. Android may contain a json lib allready but i like json simple because its simple:)
Just create a Parser object and read the String.
Then create a JSONObject by getting the "category" and so on
#user3796430 Try integrating GSON library. It will definitely help you...

how to parse the array of json string using gson in android

{
"serviceType":"IMAGE_ANDROID",
"parameters":[
[
{
"itemId":"it003376",
"itemNameInEng":"8th Chennai International Film Festival Photos",
"itemSmallImage":"http://122.183.217.134:8080/sivajitv/photos/20101216001017.jpg",
"count":"110"
},
{
"itemId":"it003375",
"itemNameInEng":"Actress Aishwarya Rai Special Photos",
"itemSmallImage":"http://122.183.217.134:8080/sivajitv/photos/20101215230917.jpg",
"count":"7"
},
{
"itemId":"it003374",
"itemNameInEng":"Actor Srikanth Flag off The Avon Greenathon Bicycle Rally ",
"itemSmallImage":"http://122.183.217.134:8080/sivajitv/photos/20101215212620.jpg",
"count":"43"
},
{
"itemId":"it003373",
"itemNameInEng":"Kadamai Kanniyam Kattupadu Movie Launch Photos",
"itemSmallImage":"http://122.183.217.134:8080/sivajitv/photos/20101213001908.jpg",
"count":"32"
},
{
"itemId":"it003372",
"itemNameInEng":"Kanden Audio Launch Photos",
"itemSmallImage":"http://122.183.217.134:8080/sivajitv/photos/20101210044334.jpg",
"count":"126"
}
]
]
}
this is my response
i want to parse the parameters from this response and i need to parse all data in parameter
please provide me correct solution
Make use of JSONArray class in org.json.JSONArray..
see this link
Accessing members of items in a JSONArray with Java
Use the org.json classes in the SDK.

Categories

Resources