how to parse the array of json string using gson in android - 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.

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

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).

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).

Categories

Resources