I am a newbie to android. I am stuck at JSON parsing.
Following is my JSON, in this, the type of menu categories will be dynamic, and I want to arrange this JSON in sectioned recycler view.
Please help me out.
This is my JSON
{
"menu_items": {
"Plat": [{
"menu_name": "jambon à l'os",
"menu_cost": "18.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
},
{
"menu_name": "tacos",
"menu_cost": "6.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
}
],
"Entrée": [{
"menu_name": "avocat",
"menu_cost": "7.50",
"menu_count": "1",
"category": "Entrée",
"menu_tax": "5.50",
"code": "€"
}]
},
"order_data": {
"order_id": "278",
"order_number": "REF-5d5e6b86",
"created_at": "1566468998",
"instructions": "",
"fullname": "Rohan Chitnis",
"phonenumber": "7610771871",
"useraddress": "Brest, France",
"image_path": "https://restau-at-home.bzh/uploads/attachments/Jellyfish_2019_08_21_10_49_21.jpg",
"wp": "pune",
"code": "€",
"promocode": null,
"promo_value": null,
"promo_type": null
},
"order_details": {
"total_items": "3",
"order_total": "31.50"
},
"promo_applied": "0",
"error": {
"code": "13",
"status": "200",
"message": "Ok"
}
}
Check this out. Create a generic response class. Extend your classes from that class. To do this properly you can look at Java Generics and Inheritance topics if you are using Java. And then search about Sectioned RecyclerView in Android. Here is a library for this.
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 newbie in Calenderview android. May be this question is duplicate but I had been tried lots of question from last three days but I am unable to make a circle on specific dates and dates will come from volley response.
I really do not know how to make this because I am able get volley response but unable to set in calendar and make a circle on that.
Here is my json response.
{
"status": "200",
"msg": "Successfully",
"response": [
{
"date": "2019-03-02",
"status": "1"
},
{
"date": "2019-03-04",
"status": "1"
},
{
"date": "2019-03-05",
"status": "1"
},
{
"date": "2019-03-06",
"status": "1"
},
{
"date": "2019-02-28",
"status": "1"
},
{
"date": "2019-02-27",
"status": "1"
},
{
"date": "2019-02-25",
"status": "1"
},
{
"date": "2019-02-21",
"status": "1"
}
]
}
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();
}
Here's the JSON where jsonObject key is dynamic :
{
"CH000032": [
{
"type": "event",
"details": {
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
}
}
],
"CH000033": [
{
"type": "event",
"details": {
"programID": "EP10000132",
"programType": "EPISODE",
"title": "A Chic Bar in Ibiza",
"seriesTitle": "Two and a Half Men",
"seasonNumber": 12,
"epsiodeNumber": 2,
"year": "2014",
"rating": "TV-14",
"durationSec": 1260,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Comedy",
"Romance"
],
"description": "Alan has second thoughts about getting married when Walden has him sign a prenup.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_crop,h_405,w_270//l_Copyright_e3jt1k/v1508831090/2AndHalfmen_splkro.jpg"
}
}
]
}
I would like to parse this JSON. Please let me know how should the class
be written with #SerializedName annotation using Retrofit.
Note : CH000032, CH000033 etc are dynamic.
You can use Map<String, ModelClassName> in your model class for dynamic like below :-
public class Data {
#SerializedName("your_key")
#Expose
private Map<String, ModelClassName> result;
//....
}
this can help to parse dynamic key in retrofit.
What you are asking will take much work , instead a better approach will be to change the structure o json to something like this -
"data": [
{
"type": "event",
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
},
{
"type": "event",
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
}]
Anybody knows how to parse the "title" content in this JSON fromat ?
JSON:
{
"news": [
{
"Id": "58710",
"c_id": "6",
"title": "റയില്വേയില് നിക്ഷേപം നടത്താന് എല്ഐസി",
"image": "http://4malayalees.com/malayalamNews/thumb/58710.jpg",
"news": " വ്യത്യസ്ത പദ്ധതികള്ക്കായി അടുത്ത അഞ്ച് വര്ഷത്തിനുള്ളില് എല്ഐസി റെയില്വേയ&"
},
{
"Id": "58551",
"c_id": "6",
"title": "മലയാളിയുടെ ന്യൂസ് പോര്ട്ടല് ഇനി റൂപര്ട്ട് മര്ഡോക്കിന് സ്വന്തം",
"image": "http://4malayalees.com/malayalamNews/thumb/58551.jpg",
"news": "സഹദ് എന്ന പേര് ഒരുപക്ഷെ മലയാളികള്ക്ക് സുപരിചിതമല്ലായിരിക്കാം, എന്നാല് മാധ്യമ ő"
}
]
}
Thanks in advance