JSONSame Key Diffrent Value. How to Show in Tablelayout? - android

Json Data display in tableLayout.
I am using retrofit2 and rxjava2 to parse the JSON data.
I have to separated data show in table.
"PartyName" is the Json key. The "PartyName" value same its display one table when "PartyName" is change its object data display in another table.
PartyName value change its created new table display data.
How to its more table create please guide me?
{
"status": 1,
"0": [
{
"Cmp_Name": "ABC",
"Partyid": "18",
"ChallanNo": "-",
"PartyName": "PURCHASE MH",
"FirmName": "PURCHASE MH",
"ChallanDate": "-",
"ChallanAmount": "0.0",
"RecieveDate": "2019-03-15 00:00:00",
"RecieveAmount": "4000.0",
"DiscountAmount": "0.0",
"TDSAmount": "0.0",
"Bank": "AXIS BANK",
"ChequeNo": null,
"ChequeDate": null
},
{
"Cmp_Name": "ABC",
"Partyid": "17",
"ChallanNo": "20",
"PartyName": "PURCHASE GJ",
"FirmName": "PURCHASE GJ",
"ChallanDate": "3/11/2019",
"ChallanAmount": "1391.0",
"RecieveDate": "2019-03-15 00:00:00",
"RecieveAmount": "1391.0",
"DiscountAmount": "0.0",
"TDSAmount": "0.0",
"Bank": "AXIS BANK",
"ChequeNo": "321456",
"ChequeDate": "2019-03-15 00:00:00"
},
{
"Cmp_Name": "ABC",
"Partyid": "17",
"ChallanNo": "20",
"PartyName": "Dharama",
"FirmName": "Dharama",
"ChallanDate": "3/11/2019",
"ChallanAmount": "1391.0",
"RecieveDate": "2019-03-15 00:00:00",
"RecieveAmount": "1391.0",
"DiscountAmount": "0.0",
"TDSAmount": "0.0",
"Bank": "AXIS BANK",
"ChequeNo": "321456",
"ChequeDate": "2019-03-15 00:00:00"
},
{
"Cmp_Name": "ABC",
"Partyid": "17",
"ChallanNo": "20",
"PartyName": "Dharama",
"FirmName": "Dharama",
"ChallanDate": "3/11/2019",
"ChallanAmount": "1391.0",
"RecieveDate": "2019-03-15 00:00:00",
"RecieveAmount": "1391.0",
"DiscountAmount": "0.0",
"TDSAmount": "0.0",
"Bank": "AXIS BANK",
"ChequeNo": "321456",
"ChequeDate": "2019-03-15 00:00:00"
}
]
}
As the image below shows, different PartyName data display in different table.
How do I display the JSON key/value pair?
Please, guide me. Thanks.

Related

how to read this JSON format [duplicate]

This question already has an answer here:
How do I parse JSON in Android? [duplicate]
3 answers
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();
}

How to fetch data from a nested array?

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");

Sorting jsonarray based on price

I tried to sort jsonarray using below code. But it not sorting the jsonarray correctly. Some values misplaced.
My Jsonarray is
{
"sTATUS": "SUCCESS",
"mSG": "Product List",
"product_list": [
{
"product_id": "5",
"product_name": "LAS VEGAS 530",
"product_model": "530",
"product_image": "uploads/main/14748883885prime.png",
"price": "18990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "68",
"product_name": "TESTAPESKER MODSPK1",
"product_model": "modspk1",
"product_image": "",
"price": "2100",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "1",
"product_name": "CAPE TOWN 940",
"product_model": "940",
"product_image": "uploads/main/14748836241prime.png",
"price": "49990",
"discount_price": "990",
"discount_percentage": "1.98%",
"offer_price": "49000",
"featured_product": "1",
"category_id": "1",
"category_name": "Speakers",
"rate": "3",
"review": "2",
"gift_id": "0"
},
{
"product_id": "10",
"product_name": "BARCELONA 230 BT",
"product_model": "230 BT",
"product_image": "uploads/main/147531739810prime.png",
"price": "10990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "1",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "3",
"product_name": "SANTA FE 530",
"product_model": "530",
"product_image": "uploads/main/14748874843prime.png",
"price": "34990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "1",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "8",
"product_name": "NEW ORLEANS 220",
"product_model": "220",
"product_image": "uploads/main/14753168598prime.png",
"price": "8990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "6",
"product_name": "SAN MARINO 330",
"product_model": "330",
"product_image": "uploads/main/14748893666prime.png",
"price": "14990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "7",
"product_name": "NEW JERSEY 220BT",
"product_model": "220BT",
"product_image": "uploads/main/14753166327prime.png",
"price": "10990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "13",
"product_name": "TOKYO 110",
"product_model": "110",
"product_image": "uploads/main/147549157513prime.png",
"price": "4990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "2"
},
{
"product_id": "2",
"product_name": "SAN ANTONIO 640",
"product_model": "640",
"product_image": "uploads/main/14748866592prime.png",
"price": "33500",
"discount_price": "500",
"discount_percentage": "1.49%",
"offer_price": "33000",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "1"
},
{
"product_id": "60",
"product_name": "PHILADELPHIA 845",
"product_model": "845",
"product_image": "uploads/main/148109520160prime.png",
"price": "54990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "1",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "12",
"product_name": "MANCHESTER 110",
"product_model": "110",
"product_image": "uploads/main/147549119212prime.png",
"price": "5990",
"discount_price": "100",
"discount_percentage": "1.67%",
"offer_price": "5890",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "11",
"product_name": "MONTEVIDEO 4010 DVD",
"product_model": "4010 DVD",
"product_image": "uploads/main/147549071111prime.png",
"price": "9990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "0",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "0"
},
{
"product_id": "4",
"product_name": "SAN DIEGO 530",
"product_model": "530",
"product_image": "uploads/main/14748877994prime.png",
"price": "31990",
"discount_price": "",
"discount_percentage": "",
"offer_price": "",
"featured_product": "1",
"category_id": "1",
"category_name": "Speakers",
"rate": "0",
"review": "0",
"gift_id": "2"
}
],
"cart_count": "0"
}
code
try {
JSONObject json=new JSONObject(cal_json);
JSONArray users = json.getJSONArray("product_list");
List<JSONObject> list = new ArrayList<JSONObject>();
for (int i = 0; i < users.length(); i++) {
list.add(users.getJSONObject(i));
}
Collections.sort( list, new Comparator<JSONObject>() {
public int compare(JSONObject a, JSONObject b) {
Long valA = null;
Long valB=null;
try {
valA = (Long) a.get("price");
valB = (Long) b.get("price");
}
catch (JSONException e) {
//do something
}
return valA.compareTo(valB);
}
});
// Collections.sort(list, new SortBasedOnMessageId());
JSONArray resultArray = new JSONArray();
for (int i = 0; i < users.length(); i++) {
resultArray.put(list.get(i));
}
Log.d("newjsonarr",resultArray.toString());
getjson(resultArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Does anyone know exact way to sort jsonarray based on string or numerical value in ascending and descending order. For any positive response thanks in advance.
Your compare class should be like this.
public int compare(JSONObject a, JSONObject b) {
Long valA = null;
Long valB=null;
try {
valA = Long.parseLong(a.get("price"));
valB = Long.parseLong(b.get("price"));
}
catch (JSONException e) {
//do something
}
return valA.compareTo(valB);
}
});

Json Data Filteration according to category

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"
}]
}

How to parse nested json array in android

i want to parse the json array which is as below. I need to get id, category etc.
"items":[{
"id": "s_A7tiCV_XQ",
"uploaded": "2014-08-30T17:52:42.000Z",
"updated": "2014-10-17T10:06:04.000Z",
"uploader": "laharimusic",
"category": "Music",
"title": "Aagadu Audio Jukebox || Super Star Mahesh Babu, Tamannaah [HD]",
"description": "Aagadu Audio Jukebox Subscribe to our Youtube Channel: http://bit.ly/1hE4KpS iTunes : http://goo.gl/SqNCep Buy Your Copy on Flipkart : http://goo.gl/4WGcuN H...",
"thumbnail":{
"sqDefault": "https://i.ytimg.com/vi/s_A7tiCV_XQ/default.jpg",
"hqDefault": "https://i.ytimg.com/vi/s_A7tiCV_XQ/hqdefault.jpg"
},
"player":{
"default": "https://www.youtube.com/watch?v=s_A7tiCV_XQ&feature=youtube_gdata_player",
"mobile": "https://m.youtube.com/details?v=s_A7tiCV_XQ"
},
"content":{
"5": "https://www.youtube.com/v/s_A7tiCV_XQ?version=3&f=videos&app=youtube_gdata",
"1": "rtsp://r3---sn-a5m7zu7r.c.youtube.com/CiILENy73wIaGQl0_ZUgtjvwsxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"6": "rtsp://r3---sn-a5m7zu7r.c.youtube.com/CiILENy73wIaGQl0_ZUgtjvwsxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
},
"duration": 1594,
"aspectRatio": "widescreen",
"rating": 4.201516,
"likeCount": "2534",
"ratingCount": 3166,
"viewCount": 1113129,
"favoriteCount": 0,
"commentCount": 610,
"accessControl":{
"comment": "allowed",
"commentVote": "allowed",
"videoRespond": "moderated",
"rate": "allowed",
"embed": "allowed",
"list": "allowed",
"autoPlay": "allowed",
"syndicate": "allowed"
}
}
You can use JSONObject and/or JSONArray checkout How to parse JSON in Android

Resources