Get the data from the JsonArray [duplicate] - android

This question already has answers here:
org.json.JSONArray cannot be converted to JSONObject
(5 answers)
Closed 5 years ago.
this is my JSON result when I print the log.I am not able to get the data from this result.Please suggest how to get the data from this result. The error shows the of type org.json.JSONArray cannot be converted to JSONObject.I know there are already many posts related to this but I can't understand how to get the data.
09-12 05:12:57.606: W/System.err(3385): org.json.JSONException: Value
[
{
"client": [
{
"office_name": "",
"is_verified": "1",
"forum_email_notification": "1",
"city": "",
"first_name": "Pratik",
"balance": "0",
"option_domain_name": "Vision",
"initials": "Mr",
"client_type": "0",
"group_credit": "0.00",
"gender": "0",
"is_approved": "1",
"domain_name": "Vision",
"client_id": "2",
"otherim": "",
"credit": "0.00",
"is_spam": "0",
"office_phone": "",
"parent_id": "0",
"domain_url": "http://example.com/vision4/sandip/v5/",
"fax": "",
"forum_signature_content": "",
"group_id": "1",
"optionB_domain_id": "1",
"country": "",
"contact_number": "",
"user_name": "test",
"email": "pratik#test.com",
"job_title": "",
"last_name": "T",
"parent_name": "",
"temporary_address": "",
"facebook_id": "",
"language": "1",
"office_contact_num": "",
"salt": "e2e838b89b16f488a901b3b364ad839e",
"zip_code": "",
"office_website": "",
"department": "",
"state": "",
"office_fax": "",
"login_as": "0",
"currency": "USD",
"permanent_address": "",
"department_access": "0",
"is_manager": "0",
"birthdate": "",
"registration_date": "1504254310",
"parent_email": "",
"password_auth": "0",
"group_name": "Default",
"netmeetingid": "",
"website": "",
"domain_id": "1",
"is_banned": "0",
"twitter_id": "",
"mobile_number": "",
"office_address": "",
"authentication": "383Y4A==",
"harvest_id": "0",
"image_path": "",
"office_email": ""
}
],
"unsolve": 3,
"solve": 0
}
]
of type org.json.JSONArray cannot be converted to JSONObject

It's because your result is an array of JSONObject and not a JSONObject itself

Related

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

Android - gson.fromJson not assigning just one field correctly

I have a json array of about 60 objects, each having about 50 fields. when I use fromJson, it is assigning the values accurately to all fields except one, "stops". "stops" is getting filled with "-1". This is the json string part for "stops" and below that is the class snippet.
"carrierid": "SG",
"stops": "0",
"seatsavailable": "1",
#SerializedName("carrierid")
#Expose
private String carrierid;
#SerializedName("stops")
#Expose
private String stops;
#SerializedName("seatsavailable")
#Expose
private String seatsavailable;
Please let me know what I might be doing wrong.
{
"data": {
"returnflights": [
{
"origin": "TRV",
"rating": 0,
"DepartureTime": "",
"flightcode": "3428",
"Group": "",
"farebasis": "LSAVER",
"FlHash": "rSG-3428",
"spicestatus": "Normal",
"holdflag": "",
"CINFO": "air-TRV-BLR-20171103--1-0-0-E-100--",
"deptime": "17:20",
"codeshare": "",
"tickettype": "e",
"ibibopartner": "spicejetnew",
"productclass": "RS",
"duration": "1h 35m",
"platingcarrier": "",
"qtype": "fbs",
"carriercode": "SG",
"flightno": "3428",
"servicetype": "J",
"fareclass": "L",
"faresequence": "5",
"destination": "BLR",
"depterminal": "1",
"carrierid": "SG",
"stops": "0",
"seatsavailable": "1",
"state": "Clean",
"arrterminal": "-",
"provider": "",
"PromotionId": "",
"fare": {
"grossamount": 5280,
"totalbasefare": 4275,
"adultbasefare": 4275,
"totalfare": 5280,
"totalsurcharge": 0,
"totaltaxes": 1005,
"adulttax": 1005,
"adulttotalfare": 5280,
"totalcommission": ""
},
"CabinClass": "",
"fareapptype": "Route",
"warnings": "Refundable",
"ArrivalTime": "",
"isallotmentmarketfare": "false",
"onwardflights": [],
"aircraftType": "DH8",
"seatingclass": "E",
"EticketFlag": "true",
"faresellkey": "0~L~~LSAVER~2022~~5~X",
"operatingcarrier": "",
"std": "2017-11-03T17:20:00",
"src": "",
"sta": "2017-11-03T18:55:00",
"ruleno": "2022",
"splitduration": "1h 35m",
"searchKey": "0:0:0:0:0:4275:0:5280:0:0:0:0:0:0:0:0:0:4275:5280:0:0:0:0:0:0",
"bookingclass": "",
"journeysellkey": "SG~3428~ ~~TRV~11/03/2017 17:20~BLR~11/03/2017 18:55~",
"airline": "spicejet",
"classofservice": "L",
"multicitysearch": "",
"depdate": "2017-11-03t1720",
"arrtime": "18:55",
"arrdate": "2017-11-03t1855",
"TravelTime": ""
},
{
"origin": "TRV",
"rating": 0,
"DepartureTime": "",
"flightcode": "874",
"Group": "",
"farebasis": "T0IP",
"FlHash": "r6E-874",
"spicestatus": "Normal",
"holdflag": "",
"CINFO": "air-TRV-BLR-20171103--1-0-0-E-100--",
"deptime": "07:50",
"codeshare": "",
"tickettype": "e",
"ibibopartner": "indigonew",
"productclass": "R",
"duration": "1h 20m",
"platingcarrier": "",
"qtype": "fbs",
"carriercode": "6E",
"flightno": "874",
"servicetype": "J",
"fareclass": "T",
"faresequence": "2",
"destination": "BLR",
"depterminal": "-",
"carrierid": "6E",
"stops": "0",
"seatsavailable": "1",
"state": "Clean",
"arrterminal": "-",
"provider": "",
"PromotionId": "",
"fare": {
"grossamount": 3808,
"totalbasefare": 2350,
"adultbasefare": 2350,
"totalfare": 3808,
"totalsurcharge": 0,
"totaltaxes": 1458,
"adulttax": 1458,
"adulttotalfare": 3808,
"totalcommission": ""
},
"CabinClass": "",
"fareapptype": "Route",
"warnings": "Refundable",
"ArrivalTime": "",
"isallotmentmarketfare": "false",
"onwardflights": [],
"aircraftType": "320",
"seatingclass": "E",
"EticketFlag": "true",
"faresellkey": "0~T~~T0IP~1059~~2~X",
"operatingcarrier": "",
"std": "2017-11-03T07:50:00",
"src": "",
"sta": "2017-11-03T09:10:00",
"ruleno": "1059",
"splitduration": "1h 20m",
"searchKey": "0:0:0:0:0:2350:0:3808:0:0:0:0:0:0:0:0:0:2350:3808:0:0:0:0:0:0",
"bookingclass": "",
"journeysellkey": "6E~ 874~ ~~TRV~11/03/2017 07:50~BLR~11/03/2017 09:10~",
"airline": "Indigo",
"classofservice": "T",
"multicitysearch": "",
"depdate": "2017-11-03t0750",
"arrtime": "09:10",
"arrdate": "2017-11-03t0910",
"TravelTime": ""
}
]
},
"data_length": 2
}

android Volley how to parse Json

android Volley how to parse Json first node "usercard" signle time and "cards" multiple time Repeat as loop getter setter Object how to use ArrayList
json Structure completed
object Store data ArrayList ;
please Help me
{
"status": "success",
"data": {
"usercard": {
"UsrID": "822",
"UsrTitle": "Mr.",
"UsrFNm": "RAI",
"UsrMNm": "S",
"UsrLNm": "Patil",
"UsrPicture": "1686cd3e8c3336deb124ee4f0e514f44_t.jpg?1476251936",
"UsrDsgntnNm": "Software Developer",
"UsrCompID": "456",
"UsrCompNm": "Miracle RAI Test",
"UsrCompRoleDesc": "",
"UsrBrID": "710",
"UsrFun": "",
"UsrCity": "1131447",
"UsrCityNm": "Mumbai",
"UsrState": "1618",
"UsrStateNm": "Maharashtra",
"UsrCty": "107",
"UsrCtyNm": "India",
"UsrBuy": "",
"UsrSell": "",
"UsrPriInd": "41",
"UsrPriIndNm": "Digital & Technology",
"UsrSecInd": "147",
"UsrSecIndNm": "Digital Services",
"UsrArtReadCnt": "0",
"UsrArtWriteCnt": "0",
"UsrDisReadCnt": "0",
"UsrDisWriteCnt": "0",
"UsrComWriteCnt": "0",
"UsrDisResCnt": "0",
"UsrJoinDate": "",
"UsrCompVerified": "0",
"UsrProCompletion": "36",
"UsrLinkCnt": "3",
"UsrSts": "A",
"UsrMobileSts": "1",
"UsrEmailSts": "1",
"UsrTyp": "2",
"EntID": "9",
"UsrAdrsLn1": "",
"UsrAdrsLn2": "",
"UsrAdrsLn3": "",
"UsrAdrsPinCd": "",
"UsrDegree": "2",
"UsrFavorites": 0,
"UsrLinkSts": "",
"UsrLoginID": "s.",
"UsrWebsite": "",
"UsrCreatedDate": "2016-10-12 05:58:58",
"UsrTwitter": "",
"UsrBlog": "",
"UsrSocialOther": "",
"SMERole": "",
"Source": "",
"DOB": "",
"UsrProfileComplected": "36"
},
"user_links": {
"total_count": "3",
"cards": [
{
"UsrID": "946",
"UsrTitle": "Ms.",
"UsrFNm": "mi",
"UsrMNm": "p",
"UsrLNm": "sawde",
"UsrPicture": "rofile/MS.png",
"UsrDsgntnNm": "Html Developer",
"UsrCompID": "494",
"UsrCompNm": "pqr",
"UsrCompRoleDesc": "",
"UsrBrID": "752",
"UsrFun": "",
"UsrCity": "1131447",
"UsrCityNm": "Mumbai",
"UsrState": "1618",
"UsrStateNm": "Maharashtra",
"UsrCty": "107",
"UsrCtyNm": "India",
"UsrBuy": "",
"UsrSell": "",
"UsrPriInd": "38",
"UsrPriIndNm": "Computers, Peripherals & Applications",
"UsrSecInd": "394",
"UsrSecIndNm": "Software and Support Services",
"UsrArtReadCnt": "0",
"UsrArtWriteCnt": "0",
"UsrDisReadCnt": "0",
"UsrDisWriteCnt": "0",
"UsrComWriteCnt": "0",
"UsrDisResCnt": "0",
"UsrJoinDate": "",
"UsrCompVerified": "0",
"UsrProCompletion": "33",
"UsrLinkCnt": "1",
"UsrSts": "A",
"UsrMobileSts": "1",
"UsrEmailSts": "1",
"UsrTyp": "2",
"EntID": "9",
"UsrAdrsLn1": "",
"UsrAdrsLn2": "",
"UsrAdrsLn3": "",
"UsrAdrsPinCd": "",
"UsrDegree": "3",
"UsrFavorites": 0,
"UsrLinkSts": ""
},
{
"UsrID": "445",
"UsrTitle": "Mr.",
"UsrFNm": "Somin",
"UsrMNm": "",
"UsrLNm": "Patil",
"UsrPicture": "hprofile/SP.png",
"UsrDsgntnNm": "Software Developer",
"UsrCompID": "271",
"UsrCompNm": "Miracle-ICICI Web Solutions Pvt. Ltd.",
"UsrCompRoleDesc": "BizDev",
"UsrBrID": "425",
"UsrFun": "Administration",
"UsrCity": "1131447",
"UsrCityNm": "Mumbai",
"UsrState": "1618",
"UsrStateNm": "Maharashtra",
"UsrCty": "107",
"UsrCtyNm": "India",
"UsrBuy": "",
"UsrSell": "",
"UsrPriInd": "41",
"UsrPriIndNm": "Digital & Technology",
"UsrSecInd": "147",
"UsrSecIndNm": "Digital Services",
"UsrArtReadCnt": "3",
"UsrArtWriteCnt": "0",
"UsrDisReadCnt": "4",
"UsrDisWriteCnt": "1",
"UsrComWriteCnt": "0",
"UsrDisResCnt": "1",
"UsrJoinDate": "2015-01-16 00:00:00",
"UsrCompVerified": "2",
"UsrProCompletion": "49",
"UsrLinkCnt": "16",
"UsrSts": "A",
"UsrMobileSts": "2",
"UsrEmailSts": "1",
"UsrTyp": "2",
"EntID": "8",
"UsrAdrsLn1": "",
"UsrAdrsLn2": "",
"UsrAdrsLn3": "",
"UsrAdrsPinCd": "",
"UsrDegree": "1",
"UsrFavorites": 0,
"UsrLinkSts": "connected"
},
{
"UsrID": "351",
"UsrTitle": "Mr.",
"UsrFNm": "Ysh",
"UsrMNm": "s",
"UsrLNm": "Yahoo",
"UsrPicture": "c40bbd1e50addca687b6e75ae658f_t.jpg?1401777417",
"UsrDsgntnNm": "Software Developer",
"UsrCompID": "212",
"UsrCompNm": "Technova",
"UsrCompRoleDesc": "",
"UsrBrID": "345",
"UsrFun": "",
"UsrCity": "1131447",
"UsrCityNm": "Mumbai",
"UsrState": "1618",
"UsrStateNm": "Maharashtra",
"UsrCty": "107",
"UsrCtyNm": "India",
"UsrBuy": "282",
"UsrSell": "345,367,368",
"UsrPriInd": "31",
"UsrPriIndNm": "Automotives, Automobiles & Vehicles",
"UsrSecInd": "391",
"UsrSecIndNm": "Automobile Parts Manufacturer",
"UsrArtReadCnt": "4",
"UsrArtWriteCnt": "1",
"UsrDisReadCnt": "8",
"UsrDisWriteCnt": "2",
"UsrComWriteCnt": "0",
"UsrDisResCnt": "2",
"UsrJoinDate": "",
"UsrCompVerified": "0",
"UsrProCompletion": "53",
"UsrLinkCnt": "8",
"UsrSts": "A",
"UsrMobileSts": "2",
"UsrEmailSts": "1",
"UsrTyp": "2",
"EntID": "7",
"UsrAdrsLn1": "Hallmark plaza",
"UsrAdrsLn2": "Nr. MIG colony",
"UsrAdrsLn3": "",
"UsrAdrsPinCd": "400051",
"UsrDegree": "2",
"UsrFavorites": 0,
"UsrLinkSts": ""
}
]
}
}
}

Parsing Boolean JSON value is always giving me false

I am getting the following JSON:
{
"ID": "479",
"id": "479",
"name": "Nishant Kango",
"last_logged": "1461225061",
"current_status": "icon-online prople-online",
"room_id": "d18f655c3fce66ca401d5f38b48c89af",
"current_user": false,
"lastmessage": [],
"userInfo": {
"id": "479",
"birthday": "1989-11-21",
"comment_privacy": "everyone",
"confirm_followers": "1",
"current_city": "Chandigarh, Chandigarh",
"follow_privacy": "everyone",
"gender": "male",
"hometown": null,
"message_privacy": "everyone",
"timeline_post_privacy": "a:1:{i:0;s:1:\"1\";}",
"feed_section": "1",
"post_privacy": "a:1:{i:0;s:1:\"5\";}",
"current_city_id": "1433",
"userSkills": ", Testing ",
"birth": {
"date": "21",
"month": "11",
"year": "1989"
},
"about": "asdasdasdas sad asd asd asd as",
"active": "1",
"avatar_id": "477",
"cover_id": "528",
"cover_position": "465",
"email": "nishantkango#yahoo.co.in",
"email_verification_key": null,
"email_verified": "0",
"language": null,
"last_logged": "1461225061",
"name": "Nishant Kango",
"time": "0",
"timestamp": "2016-04-21 13:19:40",
"timezone": null,
"type": "user",
"username": "nishant",
"verified": false,
"user_id": "479",
"first_name": "Nishant",
"middle_name": null,
"last_name": "Kango",
"dob": "1989-11-21",
"mobile": "8699022278",
"title": null,
"marital_status": "single",
"childrens": "0",
"hobbies": "Travelling",
"interests": "Technology",
"feed_back": null,
"status": "approved",
"profile_pic": null,
"date_created": "2014-10-29 00:00:00",
"date_modified": "2016-03-31 19:07:56",
"activation_code": null,
"social_login_type": "facebook",
"facebook_id": "10205077190793111",
"linked_in_id": null,
"google_id": null,
"employment_status": null,
"is_deleted": "0",
"website": "http://www.nishantkango.com",
"reference_institute": "",
"industry": "0",
"proffesion_type": "",
"url": "http://192.168.2.250:82/user/timeline/e6b34e9a0311a7f829e09d2d7c4b313e",
"cover": {
"id": "528",
"active": "1",
"album_id": "0",
"extension": "jpg",
"name": "mentordirectorybgimagesuggestions6.jpg",
"post_id": "0",
"temp": "0",
"timeline_id": "0",
"type": "photo",
"url": "common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c",
"complete_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c.jpg",
"post_url": "/index.php?tab1=story&id=0"
},
"actual_cover_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c.jpg",
"cover_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c_cover.jpg",
"avatar": {
"id": "477",
"active": "1",
"album_id": "0",
"extension": "jpg",
"name": "10390233_10205765787487598_5674312231016706405_n.jpg",
"post_id": "0",
"temp": "0",
"timeline_id": "0",
"type": "photo",
"url": "common/marvel/photos/2016/01/yS1xw_477_74071a673307ca7459bcf75fbd024e09",
"complete_url": "http://192.168.2.250:82/common/marvel/photos/2016/01/yS1xw_477_74071a673307ca7459bcf75fbd024e09.jpg",
"post_url": "/index.php?tab1=story&id=0"
},
"thumbnail_url": "http://192.168.2.250:82/common/marvel/images/default-male-avatar.png",
"avatar_url": "http://192.168.2.250:82/common/marvel/images/default-male-avatar.png",
"online": true
}
}
I have to check the online/offine status of a user using online key inside the userInfo object.I am using the following code :
JSONObject userInfo = object.getJSONObject("userInfo");
Boolean online = userInfo.getBoolean("online");
Log.e("Online",online);
When i tried to use this statement :
Log.e("online1", (String) userInfo.get("online"));
It gives me the error that java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String .
So it is sure that userInfo.get("online") is returning me some boolean value.So when i use this :
Log.e("online1", String.valueOf(userInfo.getBoolean("online")));
it gives me always false but i know i am getting true value from server in some cases.Please help me to fix the issue
Are you certain the get() function returns a boolean. I am not certain about the JSON library with Android but ones I have used will return the value as a string for which you then need to parse using Boolean.parseBoolean(value).
Hope that helps.
You can try
Boolean online = Boolean.parseBoolean(userInfo.getString("online"))
This is worked for me.
you try boolean online = userInfo.getBoolean("online"); instead of Boolean online = userInfo.getBoolean("online");
I'd recommend using the opt version instead of the get version.
The main difference is that opt will not throw a JSONException and stop parsing.
Try therefore to replace all of your getXXX() by optXXX().
Another upside is that with opt, you can specify a fallback value.
Log.e("Online", online.toString());
It takes only String.
The method e(String, String) in the type Log is not applicable for the arguments (String, boolean).

How to make a pojo class for this type of json data, because in this category and subcategory are combine relation is only through a id

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

Categories

Resources