I have to fetch the value from an array nested inside another array. I can fetch values from the first array but not able to fetch values from the second array.
I am able to read the values from the main array but I am unable to read the values from the array which is nested inside it.
JSONObject jsono = new JSONObject(res);
JSONArray jarray = jsono.getJSONArray("data");
for (int i = 0; i < jarray.length(); i++)
{
JSONObject object = jarray.getJSONObject(i);
JSONArray jarray1 = object.getJSONArray("comments_data");
for (int j = 0; j < jarray1.length(); j++)
{
JSONObject object1 = jarray1.getJSONObject(j);
String Namee = object1.getString("username");
String Ratingg = object1.getString("rating");
String Commentt = object1.getString("comment");
Comments comments1 = new Comments();
comments1.setUsername(Namee);
comments1.setRating(Ratingg);
comments1.setComments(Commentt);
comments1.setProfileimage(R.drawable.fav);
commentsList.add(comments1);
}
}
And this is my json.
{
"status": "success",
"msg": " Menu Details",
"data": [
{
"id": "1",
"rest_id": "1",
"menu_rate": "100",
"collection_time": "2:22pm",
"quantity_left": "3",
"food_type": "veg",
"img1": "",
"img2": "",
"img3": "",
"date": "",
"menu_name": "",
"comments_data": [
{
"id": "20",
"user_id": "127",
"res_id": "1",
"comment": "shreyansh s",
"date": "0000-00-00 00:00:00",
"rating": "0.0",
"username": "lucky",
"userimage": "123"
},
{
"id": "19",
"user_id": "126",
"res_id": "1",
"comment": "das",
"date": "0000-00-00 00:00:00",
"rating": "3.0",
"username": "shrey srivastava",
"userimage": "123"
},
{
"id": "18",
"user_id": "126",
"res_id": "1",
"comment": "",
"date": "0000-00-00 00:00:00",
"rating": "3.0",
"username": "shrey srivastava",
"userimage": "123"
},
{
"id": "17",
"user_id": "126",
"res_id": "1",
"comment": "sakjbdkjasbk",
"date": "0000-00-00 00:00:00",
"rating": "3.0",
"username": "shrey srivastava",
"userimage": "123"
},
{
"id": "16",
"user_id": "107",
"res_id": "1",
"comment": "hello",
"date": "0000-00-00 00:00:00",
"rating": "5",
"username": "shreyansh",
"userimage": ""
},
{
"id": "15",
"user_id": "107",
"res_id": "1",
"comment": "hello",
"date": "0000-00-00 00:00:00",
"rating": "5",
"username": "shreyansh",
"userimage": "123"
},
{
"id": "14",
"user_id": "107",
"res_id": "1",
"comment": "hello",
"date": "0000-00-00 00:00:00",
"rating": "5",
"username": "shreyansh",
"userimage": "123"
},
{
"id": "6",
"user_id": "82",
"res_id": "1",
"comment": "good",
"date": "0000-00-00 00:00:00",
"rating": "",
"username": "jaim",
"userimage": "google.com"
}
]
}
]
}
Note carefully how you JSON is built.
First, you have to query for "data". You get JSON-Array. So you traverse it by the indices.
Hence, you query for the specific index in "data" (you can use a loop, of course).
For each iteration, you get a JSONObject, and in it you query for "comments_data".
String json = "{ \"status\": \"success\", \"msg\": \" Menu Details\", \"data\": [ { \"id\": \"1\", \"rest_id\": \"1\", \"menu_rate\": \"100\", \"collection_time\": \"2:22pm\", \"quantity_left\": \"3\", \"food_type\": \"veg\", ...
JSONObject object = new JSONObject(json);
JSONObject jarray1 = jarray_data.getJSONObject(0);
JSONArray comments = jarray1.getJSONArray("comments_data");
Related
Here is sample json response
{
"status": 1,
"message": "Folders List Found.",
"list": [
{
"folder_id": "25",
"folder_unique": "MjU=",
"folder_name": "MichelleF",
"folder_url": "",
"parent_id": "1",
"user_id": "ADMN-000085",
"site_id": "ADMN-SITE-000001",
"level": "0",
"folder_added_datetime": "2019-07-31 16:43:02",
"folder_updated_datetime": "2019-07-31 16:43:02",
"status": "Active",
"children": [
{
"folder_id": "26",
"folder_unique": "MjY=",
"folder_name": "Inbox",
"folder_url": "",
"parent_id": "25",
"user_id": "ADMN-000085",
"site_id": "ADMN-SITE-000001",
"level": "0",
"folder_added_datetime": "2019-07-31 16:43:02",
"folder_updated_datetime": "2019-07-31 16:43:02",
"status": "Active",
"children": [
]
},
{
"folder_id": "27",
"folder_unique": "Mjc=",
"folder_name": "Trash",
"folder_url": "",
"parent_id": "25",
"user_id": "ADMN-000085",
"site_id": "ADMN-SITE-000001",
"level": "0",
"folder_added_datetime": "2019-07-31 16:43:02",
"folder_updated_datetime": "2019-07-31 16:43:02",
"status": "Active",
"children": [
]
}
]
}
]
}
Please give me any solution to parse this and show like this below image example recycler tree structure. This is purely dynamic adding folders and response also changed every time.
I am getting following json in response from an api:
{
"0": {
"review_id": "2",
"ad_id": "27",
"user_id": "2",
"name": "John1",
"rating": "4",
"title": "Nice object1",
"comment": "Nice",
"status": "1",
"created_date": "2016-12-12 15:46:18",
"modified_date": "2016-12-12 21:17:24",
"modified_by": "0",
"add_title": "Sell new new plot"
},
"1": {
"review_id": "3",
"ad_id": "27",
"user_id": "2",
"name": "John2",
"rating": "3",
"title": "Nice object2",
"comment": "Nice",
"status": "1",
"created_date": "2016-12-12 12:46:18",
"modified_date": "2016-12-12 21:17:24",
"modified_by": "0",
"add_title": "Sell new new plot"
},
"2": {
"review_id": "4",
"ad_id": "27",
"user_id": "2",
"name": "John3",
"rating": "4",
"title": "Nice object3",
"comment": "Nice",
"status": "1",
"created_date": "2016-12-12 11:46:18",
"modified_date": "2016-12-12 21:17:24",
"modified_by": "0",
"add_title": "Sell new new plot"
},
"status": 1,
"count": 3,
"msg": "Total Number of found records "
}
When i try to create pojo classes of this json, it does not give correct classes. Can someone please help me how to parse this json?
Thanks.
I have following pattern of JSON
{
"menu": [{
"id": "74",
"parent_id": "0",
"title": "Mobiles & Tablets",
"menu_type": "7",
"menu_icon_image": "",
"link": "#",
"childs": [{
"id": "75",
"parent_id": "74",
"title": "Mobile Phones",
"menu_type": "7",
"menu_icon_image": "",
"link": "categories/mobile-tablets"
}, {
"id": "76",
"parent_id": "74",
"title": "Mobile Accessories",
"menu_type": "7",
"menu_icon_image": "",
"link": "#"
}, {
"id": "77",
"parent_id": "74",
"title": "Tablets",
"menu_type": "7",
"menu_icon_image": "",
"link": "#"
}, {
"id": "78",
"parent_id": "74",
"title": "Audio & Video",
"menu_type": "7",
"menu_icon_image": "",
"link": "#"
}, {
"id": "79",
"parent_id": "74",
"title": "Laptops",
"menu_type": "7",
"menu_icon_image": "",
"link": "#"
}]
}, {
"id": "80",
"parent_id": "0",
"title": "WOMEN",
"menu_type": "7",
"menu_icon_image": "",
"link": "#",
"childs": [{
"id": "81",
"parent_id": "80",
"title": "GIRLS CLOTHING",
"menu_type": "7",
"menu_icon_image": "",
"link": "categories/womens-clothing"
}]
}],
"slider": [{
"id": "56",
"url": "#",
"default_image": "https://s3.amazonaws.com/atbdev/cache/advertis_img/1-fl-itwQr56f76f6df2bea.png/88e5b614324080684af061c510496d29",
"title": "Mobile Test Slider",
"image": null,
"days": null
}, {
"id": "55",
"url": "#",
"default_image": "https://s3.amazonaws.com/atbdev/cache/advertis_img/1-fl-uIXFZ56f76efbcd627.png/e5a0e22dcb0de1007c88c16db1aebb7f",
"title": "Mobile Test Slider",
"image": null,
"days": null
}, {
"id": "52",
"url": "#",
"default_image": "https://s3.amazonaws.com/atbdev/cache/advertis_img/1-fl-QULkY56f7719eb0915.png/a4d0065b56b57b7d01d44873bbdb1df8",
"title": "Mobile Test Slider",
"image": null,
"days": null
}, {
"id": "51",
"url": "#",
"default_image": "https://s3.amazonaws.com/atbdev/cache/advertis_img/1-fl-L0ROK56f7716acccf0.png/e194ea4403c4b4b7e17f1fecfa329f22",
"title": "Mobiles & Tablets",
"image": null,
"days": null
}, {
"id": "1",
"url": "",
"default_image": "https://s3.amazonaws.com/atbdev/cache/advertis_img/1-fl-qtvVk56f60fd0379b9.jpg/510c150029fd29d1d77b869d837b126e",
"title": "Slider Add",
"image": null,
"days": null
}],
"advertise": [{
"id": "54",
"usrl": "categories/mobile-tablets",
"default_image": "https://s3.amazonaws.com/atbdev/cache/advertis_img/1-fl-IWkmH56f66a2b60bde.jpg/05571c3c78954e1a461b87bd62698273",
"title": "Mobile phone home page advertise",
"image": null,
"days": null
}, {
"id": "53",
"usrl": "categories/mobile-tablets",
"default_image": "https://s3.amazonaws.com/atbdev/cache/advertis_img/3-fl-YK1pI56f5102a7237b.png/118b3074314006a62a7f10867a447be6",
"title": "Mobile phone home page advertise",
"image": null,
"days": null
}]
}
List<ExpanTitelandTotal> result1111 = new ArrayList<ExpanTitelandTotal>();
for (int i = 0; i < menuArrayList.size(); i++) {
ExpanTitelandTotal mExpanTitelandTotal11 = new ExpanTitelandTotal();
mExpanTitelandTotal11.setTitle(menuArrayList.get(i).getTitle());
mExpanTitelandTotal11.setLink(menuArrayList.get(i).getLink());
mExpanTitelandTotal11.setId(Integer.parseInt(menuArrayList.get(i).getId()));
//result1111.add(mExpanTitelandTotal11);
if (mGetDrawerAndHome.getMenu().get(i).getChilds().size() > 0) {
mExpanTitelandTotal11.setIsMultiple(true);
Log.d("mGetDrawerAndHome.getMenu().get(i).getChilds().size() ",String .valueOf(mGetDrawerAndHome.getMenu().get(i).getChilds().size()));
for (int j = 0; j < mGetDrawerAndHome.getMenu().get(i).getChilds().size(); j++)
{
Log.d("Sub Menu", mGetDrawerAndHome.getMenu().get(i).getChilds().get(j).getTitle());
ExpanTitelandTotal mExpanTitelandTotal = new ExpanTitelandTotal();
mExpanTitelandTotal.setTitle(mGetDrawerAndHome.getMenu().get(i).getChilds().get(j).getTitle());
mExpanTitelandTotal.setLink(mGetDrawerAndHome.getMenu().get(i).getChilds().get(j).getLink());
mExpanTitelandTotal.setId(Integer.parseInt(mGetDrawerAndHome.getMenu().get(i).getChilds().get(j).getId()));
result1111.add(mExpanTitelandTotal);
}
} else {
mExpanTitelandTotal11.setIsMultiple(false);
}
mExpanTitelandTotal11.setItemList(result1111);
mArrayListExpanTitelandTotals.add(mExpanTitelandTotal11);
}
listAdapterTNA = new ExpandableListAdapterTNA(ActivityDrawer.this, mArrayListExpanTitelandTotals, mExpandableListView);
mExpandableListView.setAdapter(listAdapterTNA);
mExpandableListView.setIndicatorBounds(10, 80);
mExpandableListView.setDivider(null);
above is my code and when i run this i get following output
In expandable list view two different category data store in all main category data ? any idea how can i solve this ?
Create a new List object inside the first for loop sentence:
List<ExpanTitelandTotal> result1111;
for (int i = 0; i < menuArrayList.size(); i++) {
result1111 = new ArrayList<ExpanTitelandTotal>();
/////YOUR CODE
}
I need to get the images array data how to put into the adapter and not required adapter code? Only loop and how to add that's important for me
and then after second I can access option_value array.
First all images can be accessed then after a option_value array.
{
"success": true,
"data": {
"id": "50",
"seo_h1": "",
"name": "Shirt 10001",
"manufacturer": "",
"sku": "",
"model": "10001",
"image": "http://api.yellowskydemo.com/image/cache/data/shirts/7228-500x500.jpg",
"images": [
"http://api.yellowskydemo.com/image/cache/data/shirts/13-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/302-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/5-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/205-500x500.jpg"
],
"price": "$540.00",
"rating": 0,
"description": "<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n\r\n<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n",
"attribute_groups": [],
"special": "",
"discounts": [],
"options": [
{
"name": "Size",
"type": "select",
"option_value": [
{
"image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
"price": "$50.00",
"price_prefix": "-",
"product_option_value_id": "17",
"option_value_id": "55",
"name": "L-40",
"quantity": "99"
},
{
"image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
"price": "$40.00",
"price_prefix": "+",
"product_option_value_id": "18",
"option_value_id": "57",
"name": "XXL-44",
"quantity": "100"
}
],
"required": "1",
"product_option_id": "227",
"option_id": "14"
}
],
"minimum": "1",
"meta_description": "",
"meta_keyword": "",
"tag": "",
"upc": "",
"ean": "",
"jan": "",
"isbn": "",
"mpn": "",
"location": "",
"stock_status": "Out Of Stock",
"manufacturer_id": null,
"tax_class_id": "0",
"date_available": "2014-08-12",
"weight": "0.00000000",
"weight_class_id": "1",
"length": "0.00000000",
"width": "0.00000000",
"height": "0.00000000",
"length_class_id": "1",
"subtract": "0",
"sort_order": "1",
"status": "1",
"date_added": "2014-08-13 12:05:56",
"date_modified": "2015-06-30 11:19:39",
"viewed": "8",
"weight_class": "kg",
"length_class": "cm",
"reward": "0",
"points": "0",
"category": [
{
"name": "Shirts",
"id": "59"
},
{
"name": "Casual Shirts",
"id": "60"
}
],
"quantity": "99",
"reviews": {
"review_total": "0"
}
}
}
You should really look into How to parse JsonObject / JsonArray in android.
Please put your code of also what you have tried because people are here to help solve error/problem not to do coding.
Here is code from which you can Parse your json
JSONObject jsonObject = new JSONObject();
JSONObject dataObject = jsonObject.getJSONObject("data");
JSONArray imagesArray = dataObject.getJSONArray("images");
ArrayList<String> listOfImagesUrl = new ArrayList<>();
for(int i = 0; i < imagesArray.length(); i++)
{
listOfImagesUrl.add(imagesArray.getString(i)); // listOfImages
}
// code for option value
JSONArray optionsArray = dataObject.getJSONArray("options");
for(int i = 0; i < optionsArray.length(); i++)
{
JSONObject option = optionsArray.getJSONObject(i);
JSONArray optionsValueArray = option.getJSONArray("option_value");
for(int j = 0 ; j < optionsValueArray.length(); j++)
{
JSONObject optionValueObject = optionsValueArray.getJSONObject(j);
String image = optionValueObject.getString("image");
String price = optionValueObject.getString("price");
String price_prefix = optionValueObject.getString("price_prefix");
//same like this
}
}
Here am Created the JSON Values in Static Way.
JSON Structure
{
"MatchType": {
"inningsperteam": "1",
"maxovers": "5",
"players": "11"
},
"Innings": [
{
"noballs": "0",
"BowlingDetail": [
{
"noballs": "0",
"balls": "0",
"maidens": "0",
"player": "Bowler 12",
"runs": "0",
"wickets": "0",
"bowlingposition": "1",
"wides": "0"
}
],
"byes": "0",
"battingteam": "Team 26",
"overs": "0",
"addpenaltyruns": "0",
"legbyes": "0",
"inplay": "true",
"balls": "0",
"InningsOvers": [],
"declared": "false",
"penaltyruns": "0",
"bowlingteam": "Team 46",
"runs": "0",
"wickets": "0",
"wides": "0",
"completed": "false"
}
],
"Umpire 1": "Umpire 12",
"Umpire 2": "Umpire 53",
"location": "England",
"Ground": "Oval",
"date": "20/6/2014",
"Club": [
{
"Team": {
"Player": [
{
"keeper": "false",
"captain": "false",
"firstname": "Batsman 1"
},
{
"keeper": "false",
"captain": "false",
"firstname": "Batsman 2"
}
],
"local": "1",
"name": "Team 26",
"toss": "false"
},
"local": "1",
"name": "Club1"
},
{
"Team": {
"Player": [
{
"keeper": "false",
"captain": "true",
"firstname": "Bowler 12"
}
],
"local": "2",
"name": "Team 46",
"toss": "true"
},
"local": "2",
"name": "Club2"
}
],
"CurrentPlayers": {
"bowler": "Bowler 12",
"playerA": "Batsman 1",
"playerB": "Batsman 2"
}
}
BattingDetails JSON
[
{
"BattingDetail": [
{
"balls": "0",
"sixes": "0",
"runs": "10",
"player": "Batsman 1",
"battingposition": "1",
"fours": "0"
},
{
"balls": "0",
"sixes": "0",
"runs": "0",
"player": "Batsman 2",
"battingposition": "1",
"fours": "0"
}
]
}
]
Coding
try {
json_scores = new JSONObject(test_str);
jsonArray = new JSONArray("["+test_str+"]");
json_scores = jsonArray.getJSONObject(0);
JSONArray nxt_test = new JSONArray(json_scores.getString("Innings"));
String str = new JSONArray(json_scores.getString("Innings")).getJSONObject(0).getString("BattingDetail");
JSONArray jArray = new JSONArray(str);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = new JSONObject();
json = jArray.getJSONObject(i);
if (json.getString("player").equalsIgnoreCase(player_name)) {
jArray.getJSONObject(i).put("runs", "10");
}
}
Log.v("Test Array", ""+nxt_test.put(0,new JSONObject().put("BattingDetail", jArray)));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
I want to put the BattingDetail JSON array in Innings json array in JSON Structure.am not able solve this issue.can any one know help me to solve this issue.
Take your main json data into JSONObject.
JSONObject jsobj=new JSONObject(data);
JSONArray jsarBatting=new JSONArray(battingdetail);
JSONArray jsAr=jsobj.getJSONArray("innings");
JSONObject jsobj= jsAr.getJSONObject(0);
jsobj.put("BattingDetail",jsarBatting);