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
}
}
Related
I am calling this array and I am getting value but I am not able to assign on spinner.
This is my array data.
"data": {
"id": "105",
"prod_id": "FMSP-6815",
"prod_cat_id": "4",
"prod_name": "Aakash Fish",
"prod_price": "1",
"is_avlbl": "0",
"prod_weight": "500,1000",
"prod_quantity": "0",
"prod_desc": "Eat at your own risk",
"prod_image": "medicine.jpg",
"created_at": "2019-03-26 17:33:26",
"updated_at": "2019-03-26 17:33:26",
"bone_type": "0",
"skin_type": "0",
"cut_type": "0",
"cat_name": "ready_to_eat",
"prod_attributes": [
{
"id": "15",
"name": "Size",
"attribute_options": [
{
"id": "15",
"attribute_id": "15",
"attribute_name": "size",
"prod_id": "105",
"attr_option_id": "8",
"option_price": "10",
"option_name": "Large"
}
]
},
{
"id": "14",
"name": "Clean",
"attribute_options": [
{
"id": "16",
"attribute_id": "14",
"attribute_name": "clean",
"prod_id": "105",
"attr_option_id": "6",
"option_price": "5",
"option_name": "Skinny"
},
{
"id": "20",
"attribute_id": "14",
"attribute_name": "Clean",
"prod_id": "105",
"attr_option_id": "7",
"option_price": "30",
"option_name": "White Skin"
}
]
},
{
"id": "7",
"name": "Cut",
"attribute_options": [
{
"id": "18",
"attribute_id": "7",
"attribute_name": "Cut",
"prod_id": "105",
"attr_option_id": "4",
"option_price": "20",
"option_name": "Medium"
}
]
}
]
}
This is my array
and
this is how I am calling from API.
This is my Java code. I used for loop inside for loop.
if (response.body().getDescriptionResponseData().getSuccess().equals("200")) {
attributeData = response.body().getDescriptionResponseData().getP_data().getAttributeData();
attribute_array_size = attributeData.size();
if (attributeData.size() == 0){
p_attribute.setVisibility(View.GONE);
} else {
p_attribute.setVisibility(View.VISIBLE);
// Loading attribute options
for (int j=0;j<attributeData.size();j++){
attributeOptions = response.body().getDescriptionResponseData().getP_data().getAttributeData().get(j).getAttributeOptionsData();
//String option = attributeOptions.get(j).getOption_name();
// Log.e("options",option);
dateList.clear();
dateList.add("Select");
for (int k=0;k<attributeOptions.size();k++){
String attribute_id = attributeOptions.get(k).getAttribute_id();
String option_name = attributeOptions.get(k).getOption_name();
String option_price = attributeOptions.get(k).getOption_price();
dateList.add(option_name);
//Log.e("option_id",attribute_id);
//Log.e("option_name",option_name);
// Log.e("option_price",option_price);
}
ArrayAdapter dateArrayAdapter = new ArrayAdapter(getContext(),R.layout.date_dialog, R.id.textDate, dateList);
holder.attribute_option_spinner.setAdapter(dateArrayAdapter);
}
}
}
From the question you asked I assume that your spinner shows last values. There are two problems with your code.
1- dateList.clear(); and dateList.add("Select"); should be outside for loop
2- Set spinner adapter outside for loop after all values are assigned to arrayList
Below you can check how it will go
if (response.body().getDescriptionResponseData().getSuccess().equals("200")) {
attributeData = response.body().getDescriptionResponseData().getP_data().getAttributeData();
attribute_array_size = attributeData.size();
if (attributeData.size() == 0){
p_attribute.setVisibility(View.GONE);
} else {
p_attribute.setVisibility(View.VISIBLE);
// Loading attribute options
dateList.clear();
dateList.add("Select");
for (int j=0;j<attributeData.size();j++){
attributeOptions = response.body().getDescriptionResponseData().getP_data().getAttributeData().get(j).getAttributeOptionsData();
//String option = attributeOptions.get(j).getOption_name();
// Log.e("options",option);
for (int k=0;k<attributeOptions.size();k++){
String attribute_id = attributeOptions.get(k).getAttribute_id();
String option_name = attributeOptions.get(k).getOption_name();
String option_price = attributeOptions.get(k).getOption_price();
dateList.add(option_name);
//Log.e("option_id",attribute_id);
//Log.e("option_name",option_name);
// Log.e("option_price",option_price);
}
}
if (dateList.size() > 0) {
ArrayAdapter dateArrayAdapter = new ArrayAdapter(getContext(), R.layout.date_dialog, R.id.textDate, dateList);
holder.attribute_option_spinner.setAdapter(dateArrayAdapter);
}
}
}
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");
In this json my category and subcategory are combine, how can i arse and how to set in pojo class, i wan't to set in expandable list view, please help
{
"product_categories": [{
"id": 24,
"name": "Air Cooled",
"slug": "air-cooled",
"parent": 67,
"description": "",
"display": "subcategories",
"image": "",
"count": 4
}, {
"id": 33,
"name": "Belt Driven",
"slug": "bet-driven",
"parent": 25,
"description": "",
"display": "default",
"image": "",
"count": 15
}, {
"id": 25,
"name": "CNC LATHE SPINDLE",
"slug": "cnclathespindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 15
}, {
"id": 28,
"name": "CNC MILLING SPINDLE",
"slug": "cncmillingspindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 9
}, {
"id": 29,
"name": "Motorised",
"slug": "singles",
"parent": 25,
"description": "",
"display": "default",
"image": "",
"count": 0
}, {
"id": 21,
"name": "MOTORIZED SPINDLE",
"slug": "motorizedspindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 11
}, {
"id": 30,
"name": "Water Cooled",
"slug": "water-cooled",
"parent": 67,
"description": "",
"display": "subcategories",
"image": "",
"count": 7
}, {
"id": 67,
"name": "Wood Acrylic Engraving",
"slug": "wood-acrylic-engraving",
"parent": 21,
"description": "",
"display": "products",
"image": "",
"count": 0
}]
}
In this json my category and subcategory are combine, how can i arse and how to set in pojo class, i wan't to set in expandable list view, please help
You have to parse the json response and store the different categories in different arraylist and based on the user selection of your render position you can use the selected arraylist.
You can do this by parsing the json response like this
JSONObject jsonObject = new JSONObject(response); // response is the json string that you are gettting from server
JSONArray jsonArray = jsonObject.getJSONObject("product_categories");
ArrayList<Categorydisplay> catDisList = new ArrayList<>();
ArrayList<Defaultdisplay> defaultDisList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
SomeModel someModel = new SomeModel();
if (jsonObject.has("id")) {
someModel.setId(jsonObject.getString("id"));
}
if (jsonObject.has("name")) {
someModel.setName(jsonObject.getString("name"));
}
so on.....
if(someModel.getDisplay().equals("default")){
defaultDisList.add(someModel);
}else{
catDisList.add(someModel);
}
}
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);
For example : if the given below is json of the person's profile on facebook got thorugh facebook sdk login through android app , How we will get the School Name fron the education Field in th json data in android . Please Help
Data :
{
"id": "1464730016",
"name": "Ravi Tamada",
"first_name": "Ravi",
"last_name": "Tamada",
"link": "https://www.facebook.com/ravi8x",
"username": "ravi8x",
"birthday": "12/22/1988",
"hometown": {
"id": "112158005464147",
"name": "Baruva"
},
"location": {
"id": "102186159822587",
"name": "Chennai, Tamil Nadu"
},
"bio": "Author: www.androidhive.info\r\nCo-author: www.9lessons.info",
"work": [
{
"employer": {
"id": "179366562092719",
"name": "ByteAlly"
},
"location": {
"id": "102186159822587",
"name": "Chennai, Tamil Nadu"
},
"position": {
"id": "124917314217511",
"name": "Product Head"
}
]
}
],
"favorite_athletes": [
{
"id": "18620649907",
"name": "Virat Kohli"
}
],
"education": [
{
"school": {
"id": "131587206873093",
"name": "Raghu Engineering College (REC)"
},
"degree": {
"id": "140065339390579",
"name": "B.Tech"
},
"year": {
"id": "142963519060927",
"name": "2010"
},
"type": "Graduate School",
"classes": [
{
"id": "192259410803415",
"name": "2010",
"with": [
{
"id": "584960408",
"name": "Santosh Patnaik"
}
],
"from": {
"id": "584960408",
"name": "Santosh Patnaik"
}
}
]
}
],
"gender": "male",
"relationship_status": "Single",
"website": "www.androidhive.info\nwww.9lessons.info\nwww.twitter.com/ravitamada\nwww.about.me/rv",
"timezone": 5.5,
"locale": "en_US",
"languages": [
{
"id": "106059522759137",
"name": "English"
},
{
"id": "107617475934611",
"name": "Telugu"
},
{
"id": "112969428713061",
"name": "Hindi"
},
{
"id": "343306413260",
"name": "Tamil"
}
],
"verified": true,
"updated_time": "2012-03-02T17:04:18+0000"
}
JSONObject jsonResult = new JSONObject(jsonUser);
JSONArray data = jsonResult.getJSONArray("education");
if(data != null)
{
for(int i = 0 ; i < data.length() ; i++)
{
JSONObject c = data.getJSONObject(i);
String type = c.getString("type");
if(type.equalsIgnoreCase("college"))
{
JSONObject school = c.getJSONObject("school");
String id2 = school.getString("id");
String name2 = school.getString("name");
JSONObject year = c.getJSONObject("year");
String id_y = school.getString("id");
String name_y = school.getString("name");
}
}
}
Supposing that you've this json into a jsonObject that you retrieve as response, this is the way:
// Get jsonArray 'education' from main jsonObject
JSONArray jsonArrayEducation = jsonObject.getJSONArray("education");
JSONObject jsonSchool = jsonArrayEducation.getJSONObject("school");
Note that if you are interested only at the name, you can group the two lines above into
JSONObject jsonSchool = jsonObject.getJSONArray("education").getJSONObject("school");
// get school name
String schoolName = jsonSchool.getString("name");