This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 4 years ago.
My JSON:
[
{
"productid": "3",
"name": "Potato",
"price": "15",
"quantity": "",
"stock": "0",
"description": "Fresh and good",
"image1": "upload/Potato1.jpg",
"image2": "upload/Potato2.jpg",
"image3": "upload/Potato3.jpg",
"status": "Out of Stock",
"type": "Vegetables"
},
{
"productid": "5",
"name": "Tomato",
"price": "25",
"quantity": "",
"stock": "45",
"description": "Fresh and good",
"image1": "upload/Tomato1.jpg",
"image2": "upload/Tomato2.jpg",
"image3": "upload/Tomato3.jpg",
"status": "Avaiable",
"type": "Vegetables"
},
{
"productid": "4",
"name": "Onion",
"price": "30",
"quantity": "",
"stock": "50",
"description": "Fresh and good",
"image1": "upload/Onion1.jpg",
"image2": "upload/Onion2.jpg",
"image3": "upload/Onion3.jpg",
"status": "Avaiable",
"type": "Vegetables",
"wishlistid": "43",
"userid": "10",
"flag": "1"
},
{
"productid": "6",
"name": "Carrot",
"price": "20",
"quantity": "",
"stock": "50",
"description": "Fresh and good",
"image1": "upload/Carrot1.jpg",
"image2": "upload/Carrot2.jpg",
"image3": "upload/Carrot3.jpg",
"status": "Avaiable",
"type": "Vegetables",
"wishlistid": "47",
"userid": "10",
"flag": "1"
}
]
In this first 2 json object have few column missing like userid and wishlistid and flag and remaining are having the userid, wishlistid and flag.
How can I read this with retrofit library .. because it gives me nothing as there is no userid column found in the first 2 objects.
You can parse json tow way one is manual or other using parser like Gson, Also better for both you create pojo class which help you.
String yourJsonString=jsondata;
JSONArray jsonArray= null;
try {
jsonArray = new JSONArray(yourJsonString);
if(jsonArray!=null && jsonArray.length()>0){
//Here you can parse tow way one is manual or other one using gson
//1.Manual one
for(int pos=0;pos<jsonArray.length();pos++){
JSONObject jsonObject=jsonArray.getJSONObject(pos)
//Here you can parse your object tow one is pass all value by manual
//like YourModelObject.setMethod(jsonObject.getString("keyName");
//Or other one is create model class and parse data using gson like following
/*CustomModel modelObj= new Gson().fromJson(jsonObject, new TypeToken<LocationHoModel>() {
}.getType());*/
}
//2.Using Gson
/*ArrayList<CustomModel> customModelArrayList = new Gson().fromJson(jsonArray, new TypeToken<ArrayList<CustomModel>>() {
}.getType());*/
}
} catch (JSONException e) {
e.printStackTrace();
}
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 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");
I want to know how to filter data. I got data with category name but cannot filter category wise.Here is my data:
{
"TITLE": "tea",
"PRICE": "17",
"QTY": "1",
"CAT_ID": "33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg"
},
{
"TITLE": "Coffee",
"PRICE": "102",
"QTY": "1 ",
"CAT_ID": "8",
"CAT_NAME": "MORNING SPECIAL",
"TYPE": "veg"
},
{
"TITLE": "Jeera chachh",
"PRICE": "42",
"QTY": "1",
"CAT_ID": "33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg"
},
{
"TITLE": "Roti Ghee Wali",
"PRICE": "21",
"QTY": "1",
"CAT_ID": "33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg"
},
{
"TITLE": "Paneer paratha",
"PRICE": "102",
"QTY": "1 ",
"CAT_ID": "8",
"CAT_NAME": "MORNING SPECIAL",
"TYPE": "veg"
},
{
"TITLE": "Mix paratha",
"PRICE": "102",
"QTY": "1",
"CAT_ID": "8",
"CAT_NAME": "MORNING SPECIAL",
"TYPE": "veg"
},
I already parse the data but not able to set in fragment according to category.Can anyone solve my issue.
Step 1: Store the data in database.
Step 2: Search
select * from table where CAT_NAME = 'your_filter_category'.
or better ....CAT_NAME like %your_filter_category%.
In Case of no database;
Store the Data in list.
HashMap<String, ArrayList<Item>> map = new HashMap<>();
ArrayList<Item> itemList = new ArrayList<>();
Loop on List{
Item item = fromLoop index;
if(map.contains(item.getCat())){
itemList = map.get(item.getCat());
itemList.put(item);
map.put(item.getCat(),itemList);
}else{
itemList.clear();
itemList.put(item);
map.put(item.getCat(),itemList);
}
}
//after loop you will get HashMap with keys as categories and Values as the list of selected key category.
Step 3: Publish the results.
Note: No one will do your homework here. the only thing we can do for you, we can assist you how to do.
enter your code to: JSON Formatter and you will see that something is invalid in your JSON, it has multiple root elements. Maybe you should store whole code in JSONArray like this: `
{
"features":
[{
"TITLE": "tea",
"PRICE": "17",
"QTY": "1",
"CAT_ID":"33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg" },
{
"TITLE":"Coffee",
"PRICE": "102",
"QTY": "1 ",
"CAT_ID": "8",
"CAT_NAME":"MORNING SPECIAL",
"TYPE": "veg"
}]
}
I want to know that how can i fetch data through j sons. I.I am creating like this but did not get any data?
"arr": [1]
0: {
"obj": {
"id": "24"
"dispensary_id": "24"
"item_name": "item 3"
"total_feedback": "1"
"total_item": "13"
"followers": "7"
"follow_status": "following"
"category": "Edibles"
"strain": "Indica"
"tch": "0.70"
"cbd": "13.30"
"description": "testing items"
"image": "1447508911_greenfeed-3.jpg"
"qty1": "1/8 OZ"
"price1": "100"
"qty2": "1/4 OZ"
"price2": "200"
"qty3": "1/2 OZ"
"price3": "400"
"qty4": "1 OZ"
"price4": "799"
"dispensary_first_name": "Acme Dispensary"
"dispensary_last_name": "Smith"
"company": "Acme Dispensary"
"dispensary_image": "1449182412.png"
"city": "Irvine"
"state": "CA"
"status": "1"
}-
}-
-
"result": "true"
}
Use gson library for parsing JSON data. To learn how to use it go through with this link http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
your JOSN format is wrong it should be like this
{
"obj": {
"id": "24",
"dispensary_id": "24",
"item_name": "item 3",
"total_feedback": "1",
"total_item": "13",
"followers": "7",
"follow_status": "following",
"category": "Edibles",
"strain": "Indica",
"tch": "0.70",
"cbd": "13.30",
"description": "testing items",
"image": "1447508911_greenfeed-3.jpg",
"qty1": "1/8 OZ",
"price1": "100",
"qty2": "1/4 OZ",
"price2": "200",
"qty3": "1/2 OZ",
"price3": "400",
"qty4": "1 OZ",
"price4": "799",
"dispensary_first_name": "Acme Dispensary",
"dispensary_last_name": "Smith",
"company": "Acme Dispensary",
"dispensary_image": "1449182412.png",
"city": "Irvine",
"state": "CA",
"status": "1"
}
}
for this you can check on whether your json is valid or not http://jsonlint.com/
for creating json you can use some library like
JSONObject jsonObject = new JSONObject();
jsonObject.add("key", "value");
jsonObject.add("key", "value");
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Parsing a 2D JSON array in Android and then putting them in intents and getting their values in another activity
I am making an Android applications in which I am getting the following response. I need to parse this using JSON parsing and take these to next activity:
[
{
"sno": "131",
"email": "ruma.riwaz#gmail.com",
"place": "43",
"description": "",
"image": "",
"time": "1316156532"
},
{
"sno": "130",
"email": "ruma.riwaz#gmail.com",
"place": "38",
"description": "",
"image": "",
"time": "1316153291"
},
{
"sno": "365",
"email": "ruma.riwaz#gmail.com",
"place": "86",
"description": "",
"image": "",
"time": "1318427821"
},
{
"sno": "129",
"email": "ruma.riwaz#gmail.com",
"place": "39",
"description": "",
"image": "",
"time": "1316152314"
},]
How do I fix this problem?
I think you should try searching it on Google or in StackOverFlow it self (this could be duplicate question)....
any way;
You should use either Gson or Jackson