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);
}
Related
I have two forloop in which the data is added to different ArrayList. Now my question is how do we combine these two arraylist?
Below is the code which i tried, but not working. Please give me solution for the same. TIA
try {
JSONObject object = new JSONObject(response);
status = object.getString("status");
if (status.equals("200")) {
rest.dismissProgressdialog();
JSONArray jsonArray = object.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
New_Service_Model_1 new_service_model = new New_Service_Model_1();
new_service_model.setMain_name(jsonObject.getString("main_name"));
new_service_model.setSmid(jsonObject.optInt("smid"));
JSONArray jsonArray1 = jsonObject.getJSONArray("sub_service");
//sub_services_list.clear();
sub_services_list = new ArrayList<>();
for (int j = 0; j < jsonArray1.length(); j++) {
JSONObject jsonObject1 = jsonArray1.getJSONObject(j);
New_Sub_Service_Model_1 sub_service_model = new New_Sub_Service_Model_1();
sub_service_model.setSmid(jsonObject1.optString("smid"));
sub_service_model.setSbid(jsonObject1.optInt("sbid"));
sub_service_model.setSub_name(jsonObject1.optString("sub_name"));
sub_service_model.setDesc(jsonObject1.optInt("desc"));
sub_service_model.setSt_cust(jsonObject1.optInt("st_cust"));
sub_service_model.setSt_pro(jsonObject1.optInt("st_pro"));
sub_service_model.setExist_cust(jsonObject1.optInt("exist_cust"));
sub_service_model.setExist_prov(jsonObject1.optInt("exist_prov"));
sub_services_list.add(sub_service_model);
}
new_service_model.setSub_service_list(sub_services_list);
services_list.add(new_service_model);
service_list_adapter = new Service_List_Adapter_1(this, services_list, service_costMain_interface);
rclyrview_services.setAdapter(service_list_adapter);
service_list_adapter.notifyDataSetChanged();
}
} else {
rest.dismissProgressdialog();
Toast.makeText(Service_Manage.this, "No Data found", Toast.LENGTH_SHORT).show();
}
Instead of parsing the json response manually.
Paste you json here http://www.jsonschema2pojo.org/ and use GSON or Jackson whichever is convenient for parsing the response.
There are many tutorials available which can give you a quick overview of how things can be done. Worth a try if your app has many places where you want to have Json parsing.
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'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
I am making a webservice call and the respond is in JSON.
The content which i get, is a JSONArray and looks like this:
{"jsonrpc":"2.0","id":"req-002","result":[
{"id":125043,"date":20110117,"startTime":800,"endTime":850,
"kl":[{"id":71}],"te":[{"id":23}],"su":[{"id":13}],"ro":[{"id":1}]},
{"id":125127,"date":20110117,"startTime":1055,"endTime":1145,
"kl":[{"id":71}],"te":[{"id":41}],"su":[{"id":19}],"ro":[{"id":31}]},
]}
Now i am trying to get the objects in the array, but i am only able to fetch the first array: for example i cant get the first "kl" array but i am not able to get the second one.
It always give me the error :
org.json.JSONException: Index 1 out of range [0..1)
This is what i have tried:
JSONObject jsonResult = new JSONObject(s);
// Get the result object
JSONArray arr = jsonResult.getJSONArray("result");
Log.d("Arraylänge", String.valueOf(arr.length()));
for(int i=0; i<arr.length(); i++){
JSONObject c = arr.getJSONObject(i);
anfangStunde[i] = c.getString("startTime");
endeStunde[i] = c.getString("endTime");
JSONArray klArr = c.getJSONArray("kl");
for(int j=0; i<klArr.length(); j++)
{
JSONObject k = klArr.getJSONObject(j);
klassenID[j] = k.getString("id");
}
You have a typo for(int j=0; **i**<klArr.length(); 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/