Parsing complex JSON using GSON - android

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

Related

How to filter [tag:ChatDialog] messages by custom value in Quickblox?

I am using Quickblox in my application, the problem is when trying to get chatdialog messages filtered with custom value parameter the filter not working.
chat documentation
here is mu code:
ArrayList<QBChatMessage> messages = null;
QBMessageGetBuilder qbMessageGetBuilder = new QBMessageGetBuilder();
qbMessageGetBuilder.eq("bookID", customDialog.getBookID());
qbMessageGetBuilder.sortAsc("date_sent");
try {
messages = QBRestChatService.getDialogMessages(qbChatDialog, qbMessageGetBuilder).perform();
} catch (QBResponseException e) {
e.printStackTrace();
}
response example:
{
"skip": 0,
"limit": 100,
"items": [
{
"_id": "58f6c9f4a28f9a0c7f000003",
"attachments": [],
"bookID": "-Kh9UzvClNyHuwNhLcFV",
"chat_dialog_id": "58f6bcdea0eb4766d900003c",
"created_at": "2017-04-19T02:22:44Z",
//other fields
},
{
"_id": "58f6ca12a28f9a08c5000001",
"attachments": [],
"bookID": "-KeA1pw5DGMSTg67Pbp0",
"chat_dialog_id": "58f6bcdea0eb4766d900003c",
"created_at": "2017-04-19T02:23:15Z",
//other fields
}
]
}
[Update]
I am using 3.3.0 version.
[UPDATE] 22/4/2017
I tried also #Jagapathi answer, I replaced this line
qbMessageGetBuilder.eq("bookID", customDialog.getBookID());
with this:
qbMessageGetBuilder.addRule("bookID", QueryRule.EQ, customDialog.getBookID());
but also doesn't work.
qbMessageGetBuilder.eq
"eq" is not available as per doc for comparing same values for sorting you need to use.
messageGetBuilder.addRule("bookID", QueryRule.EQ, "-Kh9UzvClNyHuwNhLcFV");

Retrofit doesn't recognize nested json response

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 .

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 build JSON Request Body?

I'm trying to build the following JSON Request Body.
It's my first time with JSON and I'm following this examples, but still struggling.
I've been taking a look to GSON but wanted to manage pure JSON first, and maybe for something so small it's not worth it to add GSON library?
Could you help me with the code?
Thanks.
{
locations:
[
{
latLng:
{
lat: 40.900799,
lng: 8.606102
}
},
{
latLng:
{
lat: 42.900799,
lng: 9.606102
}
}
]
}
EDIT:
Here is the Web Service I am trying to consume and here a request sample.
Use json objects.
Do something like that.
Don't write json by hand, you could easily do a mistake.
public JSON() throws JSONException
{
JSONArray locArr=new JSONArray();
locArr.put(createLatLng(40.900799, 8.606102));
locArr.put(createLatLng(42.900799, 9.606102));
JSONObject main=new JSONObject();
main.put("locations", locArr);
Log.d("JSON",main.toString());
}
public JSONObject createLatLng(double lat, double lng) throws JSONException
{
JSONObject latLng=new JSONObject();
latLng.put("lat",lat);
latLng.put("lon",lng);
JSONObject latLngWrap=new JSONObject();
latLngWrap.put("latLng",latLng);
return latLngWrap;
}
JSON notation requires the "" around field names. Values should only be wrapped, if they are string.
{
"locations":
[
{
"latLng":
{
"lat": 40.900799,
"lng": 8.606102
}
},
{
"latLng":
{
"lat": 42.900799,
"lng": 9.606102
}
}
]
}
You can always verify, if you JSON is correct using
http://jsonlint.com/
Expanding on maciekczwa's answer. JSON (unlike arguably XML) isn't really meant to be looked at with human eyes. It very quickly becomes very hard to see where one object starts and ends and what's wrapping what. Use his example to create your main JSON object and then fill it with whatever objects you need to. When you're all done you can convert it to string for transport very easily, without having to look at it, and doing it this way means not having to worry about what labels need to be wrapped in what kinds of quotes etc.
And eskalera is 100% correct. JSON requires all fields to be wrapped in quotes (the rules around whether or not they can be single quotes or *MUST be double quotes vary from implementation to implementation, but again, this is only relevant if you're constructing the JSON manually, as you should only do in instances where it's EXTREMELY simple (and even then... probably not).

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