hear is my JSON Response,I want to total of list price.Use retrofit framework to parse JSON.
{
"price": [
"11",
"29"
]
}
int sum = 0;
int price = gridValues.get(position).getPrice().size();
for (int j = 0; j < price; j++) {
String total =gridValues.get(position).getPrice().get(j);
sum += Integer.parseInt(total);
}
fetch data from list and set in adapter and in adepter I put above code and solve my problem
I believe your request return type is JsonObject. To get sum of your price JsonArray, you need to loop through every object of it and sum it.
JsonObject object = parse.parse(data).getAsJsonObject();
JsonArray priceArray = object.getAsJsonArray("price");
int total = 0;
for (int i = 0; i < priceArray.size(); i++) {
total += priceArray.get(i).getAsInt();//reading (as int) each object of array
}
System.out.println("Total is " + total);
Related
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
This function generate dummy data for this Structure .
private void createDummyData() {
for (int i = 1; i <= 10; i++) {
VerticalDataModel dm = new VerticalDataModel();
dm.setSectionTitle("Section " + i);
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 10; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}
dm.setSingleItemModelha(singleItem);
allSampleData.add(dm);
}
Know for generating this data from internet , i need json structor for this work .(I use volley library)
Thanks <3
{
data : [
{
"title":"section1",
"items":[{"name":"item1","url":"url1"},{"name":"item2","url":"url2"}]
},
{
"title":"section2",
"items":[{"name":"item3","url":"url3"},{"name":"item4","url":"url4"}]
}
]
}
so basically you have array of objects which contains title and items as its property. Again items is another array of objects which contains name and url as its property.
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 have following data from JSON
{
"MenuName": "starter dish",
"SubMenu": [
"pizza dish1",
"pizza dish2"
],
"Price": [
"100",
"110"
]
},
From here i can easily fetch data from "Menuname" key as starter dish but when I fetch data from "Submenu" I get whole string as ["pizza dish1", "pizza dish2"].
Please suggest me way to differentiate both pizza dish1 and pizza dish2
Submenu and Price is not String it is JSONArray so you will need to use for-loop to get all values from Submenu JSONArray as:
JSONArray jsonsubmenu=yourjsonobject.getJSONArray("Submenu");
for(int i=0;i < jsonsubmenu.length();i++){
// get all values from jsonsubmenu JSONArray..
String str_value=jsonsubmenu.optString(i);
....
}
try this
for (int i = 0; i < jsonArray.length(); i++) {
jsonArray.getJSONObject(i).getInt("SubMenu");
}
you can use this link for creating POGO class for your response. It will automatically generate class for your response.
Use google GSON library to parse your response.
or you can simply create a JSON Array or Objects to parse it. in your case it is JSON Object or JSON arrays.
Hi There in that senarerio you have to use
JSONArray _submenu = object.getJSONArray("SubMenu");
for (int i = 0; i < _submenu.length(); i++) {
String text = _submenu.getString(i);
}
JSONArray _price = object.getJSONArray("Price");
for (int i = 0; i < _price.length(); i++) {
String text = _price.getString(i);
}
You can retrieve array values and store it in string[] and use them as per your need. i.e., as follows..
try {
JSONObject jObject = new JSONObject(jsonString);
JSONArray jSubmenu = jObject.getJSONArray("SubMenu");
subMenu = new String[jSubmenu.length()];
for (int i = 0; i < subMenu.length; i++) {
subMenu[i] = jSubmenu.getString(i);
}
JSONArray jPrice = jObject.getJSONArray("Price");
price = new String[jPrice.length()];
for (int i = 0; i < price.length; i++) {
price[i] = jPrice.getString(i);
}
} catch (Exception e) {
// TODO: handle exception
}
Just to throw in a quickie - read up on, and use GSON.
For simple small jobs I find it is the best. Not the fastest running for complex, or long structures, but really quick on the dev side.
Here's the link: google-gson