I'm trying to parse a JSON which contains:
"images": [
"https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg",
"https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg",
"http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg"
]
The problem is that I don't know how to get the string of elements inside the "images" node
My code currently looks like:
if(c.has("images") && !c.isNull("images")){
JSONArray imagenes = jsonObj.getJSONArray("images");
for (int j = 0; j < imagenes.length(); j++) {
JSONObject m = imagenes.getJSONObject(i);
}
}
How can I get each string in the array without using a "key"?
I don't know what to do with "M" next.
You want a string, not an object. Strings don't have nested keys.
Use getString(i) method instead of getJSONObject(i)
Your images array contains with string values not with json object. So you need to get string instead of jsonObject.
if(c.has("images") && !c.isNull("images")){
JSONArray imagenes = jsonObj.getJSONArray("images");
for (int j = 0; j < imagenes.length(); j++) {
String imgURL = imagenes.optString(i);
System.out.println(imgURL);
}
}
Output:
https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg
https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg
http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg
Related
Writing Android Application, Need to Parse JSON Data which is nested
Below is Actual data received from http.
Currently using Volley,
JSONArray dataJSONArray = response.getJSONArray("data");
Need example code to parse and display data from below mentioned JSON data
{"data":[{"counting_area_id":3,"name":"Utilization","parking_area_id":1, "free":3,"total":200,"location_latitude":null,"location_longitude":null,"places":10,
"children":[{"counting_area_id":1,"name":"Basement 1","parking_area_id":1, "free":0,"total":116,"location_latitude":null,"location_longitude":null,"places":0,
"children":[]},{"counting_area_id":73,"name":"Basement 2","parking_area_id":1, "free":3,"total":121,"location_latitude":null,"location_longitude":null,"places":3,
"children":[]}]}]}
You need to passed your data to JSONArray and parse through it using for loop ,like below code :
JSONArray jsonArray = response.getJSONArray("data");//getting array
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject= jsonArray.getJSONObject(i);//getting first element
String id= jsonobject.getString("counting_area_id");//value of counting_area_id ,get all value in same way i.e location,places etc.
System.out.println(id);
JSONArray jsonObject1= object.getJSONArray("children"); //getting children array
for (int j = 0; j < jsonObject1.length(); j++) {
JSONObject object1 = jsonObject1.getJSONObject(j);
String id= object1.getString("counting_area_id");//same as before
}
}
I face this problem, from the API I'm getting an extens JSON with one object called coordinates, this object is an array that includes an array o array.
To be more clear look at this example:
"coordinates": [
[
[
-0.087118,
51.508823
]
Or in this pic you can see it better
So now I'm trying to parse it using GSON and I'm not being success. Can anyone give me an idea how to create the class for this?
Thank you
public class Coordinate extends ArrayList<Arraylist<Arraylist<float>>>{
}
I can give you an example, where you wouldn't use Gson, but you'll get the idea.
Lets say that you put your JSON responce in a String called json
String json = ""coordinates": [[[-0.087118,51.508823]";
JSONArray firstArray = new JSONArray(json);
for (int i =0; i < firstArray.length(); i++){
JSONArray secondArray = firstArray.getJSONArray(i);
for (int j =0; i < secondArray.length(); j++){
JSONArray thirdArray = secondArray.JSONArray(j);
for (int q =0; i < thirdArray.length(); q++){
int x = thirdArray.getInteger("0");
int y = thirdArray.getInteger("1");
}
}
}
Hey every one i want to get data from JSON but i could not get.. because the service come array inside array so anyone have Idea to solve, please
"services":[{"main_category_id":"78","main_category_name":"Repairing",
"product": [{"product_id":"3","product_name":"Washing Machine",
i have code like but only product data comes but service data exception
JSONArray contentServices = dataContent
.getJSONArray("services");
JSONObject Service_items = contentServices.getJSONObject(0);
List<CompanyServicesBean> jsonServicesList = new ArrayList<CompanyServicesBean>();
for (int j = 0; j < Service_items.length(); j++) {
JSONObject services = items.getJSONObject(j);
CompanyServicesBean serviceBean = new CompanyServicesBean();
serviceBean.setmain_category_id(services
.getString("main_category_id"));
ServiceBean.setmain_category_name(services
.getString("main_category_name"));
serviceBean.setproduct_id(services
.getString("product_id"));
serviceBean.setproduct_name(services
.getString("product_name"));
serviceBean.setduration_type(services .getString("duration_type"));
serviceBean.setduration_min(services
.getString("duration_min"));
}
You have a services array of Json Object
Inside each Json Object theres is another array product
So After getting services
for (int j = 0; j < Service_items.length(); j++) {
JSONObject services = items.getJSONObject(j);
CompanyServicesBean serviceBean = new CompanyServicesBean();
}
You need to get product array from serviceBean Object like this
for (int j = 0; j < services.getJsonArray("product").length(); j++) {
JSONObject product = items.getJSONObject(j);
}
I want below process on with out editing on server side
I followed this example
but over there he is calling data with one json object from a url
called "worldpopulation",..
but i have a Json data which is having lot of Json objects and arrays,.. like below code
{ "getting data":"ok"
"Todays":population
{"results"{[
"worldpopulation":
[
{
"rank":1,"country":"China",
"population":"1,354,040,000",
"flag":["http://www.androidbegin.com/tutorial/flag/china.png"]
},
{
"population":{
[
"countrypulation" {
"rank":2,"country":"India",
"population":"1,210,193,422",
"flag":["http://www.androidbegin.com/tutorial/flag/india.png"]
},
like that i have a lot of data i want this data in that above example. Without changing any thing on server side,.. or Restful service,..
thank you
response
JSONObject JObject = new JSONObject(response);
String getting_data = JObject.getstring("getting data");
String Todays= JObject.getstring("Todays");
JsonArray results =JObject.getjsonarray("results");
for(int i = 0 ; i < results.length(); i++){
JsonObject resultsobject =results.getjsonobject(i);
JsonArray worldpopulation
=resultsobject.getjsonarray("worldpopulation");
for(int j = 0 ; j < worldpopulation.length(); j++){
JsonObject worldpopulationobject =worldpopulation.getjsonobject(j);
JsonObject emptyobject =worldpopulationobject.getjsonobject("");
String rank=emptyobject .getstring("rank");
String population=emptyobject .getstring("population");
JsonArray flagarray =countrypulation.getjsonarray(flag);
for(int n = 0 ; n < flagarray.length(); n++){
JsonObject flagarrayobject =flag1array.getjsonobject(n);
String flag=flag1arrayobject .getstring("flag");
}
JsonObject populationobject
=worldpopulationobject.getjsonobject("population");
JsonArray emptyarray =JObject.getjsonarray("");
for(int k = 0 ; k < emptyarray.length(); k++){
JsonObject emptyarrayobject =emptyarray.getjsonobject(k);
JsonObject
countrypulation=emptyarrayobject.getjsonobject("countrypulation");
String rank1=countrypulation .getstring("rank");
String country=countrypulation .getstring("country");
String population1=countrypulation .getstring("population");
JsonArray flag1array =countrypulation.getjsonarray(flag);
for(int m = 0 ; m < flag1array.length(); m++){
JsonObject flag1arrayobject =flag1array.getjsonobject(m);
String flag1=flag1arrayobject .getstring("flag");
}
}
}
}
You parse
{
...
}
as JSONObject and
[
...
]
as JSONArray. JSONArray contains your objects. From the JSONObject you can retrieve the property values.
JSONArray response = new JSONArray('your json string');
for (int i = 0; i < response.length(); i++) {
response.getJSONObject(i).getString("property"));
}
BTW your json is invalid, event if you close the missing brackets. Check it here http://jsonlint.com/
I'm making an app for android which makes a call to a webservice that returns a json with ARRAY as a parameter, I can go through all the settings and save them easily.
The problem is when I get the array that I returned within the JSON object.
Example of JSON:
[{"codigoArticulo":"0001","nombreArticulo":"CHULETAS DE CORDERO","factorVentasDefecto":"KG","precio":21.95,"factoresDeVenta":["KG","UN"]},{"codigoArticulo":"0007","nombreArticulo":"FALDETA DE CORDERO","factorVentasDefecto":"KG","precio":11.95,"factoresDeVenta":["KG","FL"]}]
I can save "codigoArticulo", "nombreArticulo", "factorVentasDefecto" and "precio easily, BUT i don't know how i can save "factoresDeVenta".
i have this code:
JSONArray resparray = new JSONArray(JSONdevuelto);
for (int i = 0; i < resparray.length(); i++) {
JSONObject respJSON = resparray.getJSONObject(i);
int IDArticulo = respJSON.getInt("codigoArticulo");
String NombreArticulo = respJSON.getString("nombreArticulo");
String FactordeVenta = respJSON.getString("factorVentasDefecto");
int PrecioArticulo = respJSON.getInt("precio");
}
How i can save in one array the variables on "factoresDeVenta"?
I try
String[] Factores = respJSON.getJSONArray("factoresDeVenta");
but no works because are incompatible types.
I need the array to make later a Spinner
Thank you.
factoresDeVenta is an JSONArray inside JSONObject so you will need to use getJSONArray or optJSONArray and use loop for getting values from JSONArray:
JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");
for (int i = 0; i < jArray.length(); i++) {
String str_value=jArray.optString(i); //<< jget value from jArray
}
JSONArray jArray = respJSON.getJSONArray("factoresDeVenta");
It would give you the array of data stored in factoresDeVenta, which you can again traverse to get the individual elements from that array.
You can store in ArrayList<String>
ArrayList<String> arrStr = new ArrayList<String>();
JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");
for (int i = 0; i < jArray.length(); i++) {
arrStr.add(jArray.getString(i));
}
And then you can convert arraylist to string array
String[] Factores = arrStr.toArray(new String[arrStr.size()]);