Parsing Boolean JSON value is always giving me false - android

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).

Related

how to remove the parameters inside the list ..and make new list android kotlin

I am having a json response and i want store that response of data to room table .i don't need some parameters inside data ( like slug, row_order , tax_id , etc .. ) how to remove that and make new list
{
"data": [
{
"id": "2637",
"row_order": "0",
"name": "Chennai Kichadi Ponni Rice",
"tax_id": "1",
"slug": "chennai-kichadi-ponni-rice-1",
"category_id": "52",
"subcategory_id": "196",
"indicator": "1",
"manufacturer": "",
"made_in": "Chennai Kichadi Ponni Rice",
"return_status": "1",
"cancelable_status": "1",
"till_status": "received",
"image": "http://www.pasumai.co.in/upload/images/5718-2021-12-18.png",
"status": "1",
"popular": "0",
"date_added": "2021-12-18 06:26:12",
"return_max_days": "0",
"tax": "0",
"price": "1599",
"tax_title": "GST",
"tax_percentage": "0",
"is_favorite": false,
"is_notify_me": false,
"variants": [
{
"id": "4726",
"product_id": "2637",
"cart_count": "0",
"is_notify_me": false
}
]
},
{
"id": "2648",
"row_order": "1",
"name": "Double Deer Basmati Rice Gold",
"tax_id": "9",
"slug": "double-deer-basmati-rice-gold",
"category_id": "52",
"subcategory_id": "196",
"indicator": "1",
"manufacturer": "",
"made_in": "Double Deer Basmati Rice Gold",
"return_status": "1",
"cancelable_status": "1",
"till_status": "received",
"image": "http://www.pasumai.co.in/upload/images/3330-2021-12-21.png",
"status": "1",
"popular": "0",
"date_added": "2021-12-19 00:23:28",
"return_max_days": "0",
"tax": "5",
"price": "279",
"tax_title": "GST",
"tax_percentage": "5",
"is_favorite": false,
"is_notify_me": false,
"variants": [
{
"id": "4755",
"product_id": "2648",
"cart_count": "0",
"is_notify_me": false
}
]
}
]
}
I want the new list as like this
{
"data": [
{
"id": "2637",
"name": "Chennai Kichadi Ponni Rice",
"subcategory_id": "196",
"image": "http://www.pasumai.co.in/upload/images/5718-2021-12-18.png",
"is_favorite": false,
},
{
"id": "2648",
"name": "Double Deer Basmati Rice Gold",
"subcategory_id": "196",
"image": "http://www.pasumai.co.in/upload/images/3330-2021-12-21.png",
"is_favorite": false,
}
]
}
Is there any method to filter ?? if yes ..please explain me with code
i want to add the new list to room table...so that i need to cut some unwanted parameters . i don't know how to do that
You need to deserialize JSON to object, for deserialization you can use
com.google.gson add to gradle:
dependencies {
implementation 'com.google.code.gson:gson:2.9.1'
}
create your data class
data class Order(
val id:String,
val name:String,
val image: String,
#SerializedName("subcategory_id")
val subcategoryId: String,
#SerializedName("is_favorite")
val isFavorite: Boolean
)
and finally deserialize your incoming data from network
val responseFromNetwork = getDataFromNetwork() //here you get your json from network
val gson = Gson()
val ordersType = object : TypeToken<List<Order>>() {}.type
val itemList = gson.fromJson<List<Order>>(responseFromNetwork , ordersType)// will be deserialized only field what are in data class another one will be ignored
and finally add to DB your data

How to parse this tree structured json response to form recycler tree view in android?

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.

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 pass object of json to other activity

I have this json
{
"results": [{
"gender": "male",
"name": {
"title": "mr",
"first": "isidro",
"last": "nogueira"
},
"location": {
"street": "3663 rua amazonas ",
"city": "mesquita",
"state": "rio de janeiro",
"postcode": 27744
},
"email": "isidro.nogueira#example.com",
"login": {
"username": "orangegoose315",
"password": "redalert",
"salt": "Dd3U7U03",
"md5": "325f1e8753222facba6ee63a5e2451de",
"sha1": "a63a0c544a956e235330b0ed76d11d3ad17c7979",
"sha256": "ddf18ffff89684617b2c7b8c5c175b9570cb6005916d9b4f3face802bd4f13a1"
},
"dob": "1967-04-23 04:58:32",
"registered": "2016-05-08 01:56:44",
"phone": "(11) 8368-8583",
"cell": "(17) 9743-3074",
"id": {
"name": "",
"value": null
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/57.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/57.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/57.jpg"
},
"nat": "BR"
}],
"info": {
"seed": "5b3dff5c25caaf7f",
"results": 1,
"page": 1,
"version": "1.1"
}
}
This data is of just one user. I have 50 of them.
I am still in learning stage and what I have done is that I have successfully listed all user in an activity. Now what I want to do is when I click on a user it should show it's related details in next activity...
List Users->Select Users->Show selected user details
I still don't know how to send data to other activity and display it.

How to obtain/parse JSONObject data with GSON

I am trying to use retrofit with android, which is kind of forcing me to use gson.
I am trying to get the data from the payload["response"]["docs"][0]["web_url"] in my code.
Issue : the key getResponse(), from below model class, always gives me {}. Whats the right way to handle this in android? Also note, the getCopyright() & getStatus() are working fine.
I am also providing the sample payload.
I have the model class like this :
public class ArticleSearchResponseModel {
#SerializedName("status") private String status;
#SerializedName("copyright") private String copyright;
#SerializedName("response") private JSONObject response;
public String getStatus() {
return status;
}
public String getCopyright() {
return copyright;
}
public JSONObject getResponse() {
return response;
}
public void setStatus(String status) {
this.status = status;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public void setResponse(JSONObject response) {
this.response = response;
}
}
My payload :
{
"response": {
"meta": {
"hits": 15987906,
"time": 108,
"offset": 0
},
"docs": [
{
"web_url": "http://www.nytimes.com/2016/11/13/insider/gender-issues-in-sharp-focus-at-the-times.html",
"snippet": "Susan Chira, senior correspondent for gender issues, and Susan Dominus, a Times Magazine writer, talk about men, women and coverage of their changing roles.",
"lead_paragraph": "Susan Chira, senior correspondent for gender issues, and Susan Dominus, a Times Magazine writer, talk about men, women and coverage of their changing roles.",
"abstract": null,
"print_page": null,
"blog": [],
"source": "The New York Times",
"multimedia": [
{
"width": 190,
"url": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbWide.jpg",
"height": 126,
"subtype": "wide",
"legacy": {
"wide": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbWide.jpg",
"wideheight": "126",
"widewidth": "190"
},
"type": "image"
},
{
"width": 600,
"url": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-articleLarge.jpg",
"height": 400,
"subtype": "xlarge",
"legacy": {
"xlargewidth": "600",
"xlarge": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-articleLarge.jpg",
"xlargeheight": "400"
},
"type": "image"
},
{
"width": 75,
"url": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbStandard.jpg",
"height": 75,
"subtype": "thumbnail",
"legacy": {
"thumbnailheight": "75",
"thumbnail": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbStandard.jpg",
"thumbnailwidth": "75"
},
"type": "image"
}
],
"headline": {
"main": "Gender Issues in Sharp Focus at The Times",
"content_kicker": "Insider Podcasts",
"kicker": "Insider Podcast"
},
"keywords": [
{
"rank": "1",
"is_major": "N",
"name": "subject",
"value": "Presidential Election of 2016"
},
{
"rank": "2",
"is_major": "N",
"name": "subject",
"value": "Women and Girls"
},
{
"rank": "3",
"is_major": "N",
"name": "subject",
"value": "Gender"
},
{
"rank": "4",
"is_major": "N",
"name": "subject",
"value": "Men and Boys"
},
{
"rank": "5",
"is_major": "N",
"name": "persons",
"value": "Trump, Donald J"
}
],
"pub_date": "2016-11-13T00:00:00Z",
"document_type": "article",
"news_desk": "Insider",
"section_name": "Times Insider",
"subsection_name": null,
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Susan",
"rank": 1,
"lastname": "LEHMAN"
}
],
"original": "By SUSAN LEHMAN"
},
"type_of_material": "News",
"_id": "58011d9b253f0a0e44966174",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://www.nytimes.com/2016/10/25/realestate/doors-of-manhattan.html",
"snippet": "Even though they stand between us and the chaos of the city, the borough’s distinctive doors are easy to overlook.",
"lead_paragraph": "Even though they stand between us and the chaos of the city, the borough’s distinctive doors are easy to overlook.",
"abstract": null,
"print_page": null,
"blog": [],
"source": "The New York Times",
"multimedia": [],
"headline": {
"main": "Doors of Manhattan",
"content_kicker": "Voyeur",
"kicker": "Voyeur"
},
"keywords": [
{
"rank": "1",
"is_major": "N",
"name": "subject",
"value": "Real Estate and Housing (Residential)"
},
{
"rank": "2",
"is_major": "N",
"name": "subject",
"value": "Architecture"
},
{
"rank": "3",
"is_major": "N",
"name": "glocations",
"value": "Upper West Side (Manhattan, NY)"
},
{
"rank": "4",
"is_major": "N",
"name": "glocations",
"value": "Upper East Side (Manhattan, NY)"
}
],
"pub_date": "2016-10-25T00:00:00Z",
"document_type": "article",
"news_desk": "RealEstate",
"section_name": "Real Estate",
"subsection_name": null,
"byline": {
"person": [
{
"firstname": "Photographs",
"middlename": "George",
"lastname": "ETHEREDGE",
"rank": 1,
"role": "reported",
"organization": ""
}
],
"original": "Photographs by GEORGE ETHEREDGE for THE NEW YORK TIMES"
},
"type_of_material": "News",
"_id": "57e94175253f0a6bb40cbffc",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://cooking.nytimes.com/recipes/1018356-celery-toasts",
"snippet": "This was the first recipe that the chef and writer Gabrielle Hamilton brought to The Times as an Eat columnist for the Sunday magazine in 2016, a snack-tray-sandwich version of a celery-and-fennel salad served at her restaurant, Prune, in the East...",
"lead_paragraph": "This was the first recipe that the chef and writer Gabrielle Hamilton brought to The Times as an Eat columnist for the Sunday magazine in 2016, a snack-tray-sandwich version of a celery-and-fennel salad served at her restaurant, Prune, in the East Village. It calls for thick, white toasted Pullman bread spread wall to wall with unsalted butter, with slices of blue cheese neatly laid on top, below a mound of shaved celery and thinly sliced scallions dressed in garlic, olive oil, lemon juice and salt, and the whole shebang dusted in ground black pepper before being cut in halves or quarters. \"The ingredients come from the grocery store,\" she wrote in her column. \"These toasts are not expensive or intimidating, but they are outstanding.\"",
"abstract": null,
"print_page": null,
"blog": [],
"source": "du_recipe",
"multimedia": [
{
"credit": "Davide Luciano for The New York Times. Food stylist: Michelle Gatton. Prop stylist: Alex Brannian.",
"url": "images/2016/10/23/magazine/23eat/23eat-thumbStandard-v2.jpg",
"rank": "1",
"height": 75,
"subtype": "thumbnail",
"legacy": [],
"caption": "Celery Toasts",
"type": "image",
"width": 75
},
{
"credit": "Davide Luciano for The New York Times. Food stylist: Michelle Gatton. Prop stylist: Alex Brannian.",
"url": "images/2016/10/23/magazine/23eat/23mag-23eat-t_CA1-articleLarge.jpg",
"rank": "1",
"height": 400,
"subtype": "large",
"legacy": [],
"caption": "Celery Toasts",
"type": "image",
"width": 600
}
],
"headline": {
"main": "A Snack Tray to Gather the Family Around",
"name": "Celery Toasts"
},
"keywords": [],
"pub_date": "2016-10-23T00:00:00Z",
"document_type": "recipe",
"news_desk": null,
"section_name": null,
"subsection_name": null,
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Gabrielle",
"rank": 1,
"lastname": "Hamilton"
}
],
"original": "Gabrielle Hamilton"
},
"type_of_material": "Recipe",
"_id": "580696f1253f0a7d028b0864",
"word_count": "101",
"slideshow_credits": null
},
{
"web_url": "http://www.nytimes.com/2016/10/23/books/review/the-story-behind-this-weeks-best-sellers.html",
"snippet": "Ruth Bader Ginsburg, whose anthology “My Own Words” is No. 12 in hardcover nonfiction, has something in common with Notorious B.I.G.",
"lead_paragraph": "Ruth Bader Ginsburg, whose anthology “My Own Words” is No. 12 in hardcover nonfiction, has something in common with Notorious B.I.G.",
"abstract": null,
"print_page": "30",
"blog": [],
"source": "The New York Times",
"multimedia": [
{
"width": 190,
"url": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbWide.jpg",
"height": 126,
"subtype": "wide",
"legacy": {
"wide": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbWide.jpg",
"wideheight": "126",
"widewidth": "190"
},
"type": "image"
},
{
"width": 75,
"url": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbStandard.jpg",
"height": 75,
"subtype": "thumbnail",
"legacy": {
"thumbnailheight": "75",
"thumbnail": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbStandard.jpg",
"thumbnailwidth": "75"
},
"type": "image"
}
],
"headline": {
"main": "The Story Behind This Week’s Best Sellers"
},
"keywords": [
{
"rank": "1",
"is_major": "N",
"name": "subject",
"value": "Books and Literature"
},
{
"rank": "2",
"is_major": "N",
"name": "persons",
"value": "Ginsburg, Ruth Bader"
}
],
"pub_date": "2016-10-23T00:00:00Z",
"document_type": "article",
"news_desk": "BookReview",
"section_name": "Books",
"subsection_name": "Book Review",
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Gregory",
"rank": 1,
"lastname": "COWLES"
}
],
"original": "By GREGORY COWLES"
},
"type_of_material": "News",
"_id": "5800f337253f0a7985625011",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://www.nytimes.com/2016/10/23/books/review/ngugi-wa-thiongo-birth-of-a-dream-weaver.html",
"snippet": "A Kenyan considers his early years as a student and writer.",
"lead_paragraph": "A Kenyan considers his early years as a student and writer.",
"abstract": null,
"print_page": "25",
"blog": [],
"source": "The New York Times",
"multimedia": [
{
"width": 190,
"url": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbWide.jpg",
"height": 126,
"subtype": "wide",
"legacy": {
"wide": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbWide.jpg",
"wideheight": "126",
"widewidth": "190"
},
"type": "image"
},
{
"width": 600,
"url": "images/2016/10/23/books/review/23Wrong/23Wrong-articleLarge.jpg",
"height": 400,
"subtype": "xlarge",
"legacy": {
"xlargewidth": "600",
"xlarge": "images/2016/10/23/books/review/23Wrong/23Wrong-articleLarge.jpg",
"xlargeheight": "400"
},
"type": "image"
},
{
"width": 75,
"url": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbStandard.jpg",
"height": 75,
"subtype": "thumbnail",
"legacy": {
"thumbnailheight": "75",
"thumbnail": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbStandard.jpg",
"thumbnailwidth": "75"
},
"type": "image"
}
],
"headline": {
"main": "Ngugi wa Thiong’o on Starting Out as a Writer",
"content_kicker": "Nonfiction",
"kicker": "Nonfiction",
"print_headline": "A Long Look Back"
},
"keywords": [
{
"rank": "2",
"is_major": "N",
"name": "persons",
"value": "Thiong'o, Ngugi wa"
},
{
"rank": "3",
"is_major": "N",
"name": "subject",
"value": "Books and Literature"
},
{
"rank": "4",
"is_major": "N",
"name": "subject",
"value": "Writing and Writers"
}
],
"pub_date": "2016-10-23T00:00:00Z",
"document_type": "article",
"news_desk": "BookReview",
"section_name": "Books",
"subsection_name": "Book Review",
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Michela",
"rank": 1,
"lastname": "WRONG"
}
],
"original": "By MICHELA WRONG"
},
"type_of_material": "Review",
"_id": "57ff4d1d253f0a6b9720abd2",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://cooking.nytimes.com/recipes/1018347-lemon-drizzle-cake",
"snippet": "This light and moist lemon poundcake has a crunchy sugar glaze that crystallizes on top, giving a contrasting texture to the soft crumb underneath. It’s an easy-to-make, crowd-pleasing cake that’s excellent on its own but takes well to...",
"lead_paragraph": "This light and moist lemon poundcake has a crunchy sugar glaze that crystallizes on top, giving a contrasting texture to the soft crumb underneath. It’s an easy-to-make, crowd-pleasing cake that’s excellent on its own but takes well to embellishments. A scoop of ice cream or sorbet, fruit compote and-or lemon curd are all wonderful alongside.",
"abstract": null,
"print_page": null,
"blog": [],
"source": "du_recipe",
"multimedia": [
{
"credit": "Andrew Scrivani for The New York Times",
"url": "images/2016/10/19/dining/19BAKEOFF2/19BAKEOFF2-thumbStandard.jpg",
"rank": "1",
"height": 75,
"subtype": "thumbnail",
"legacy": [],
"type": "image",
"width": 75
},
{
"credit": "Andrew Scrivani for The New York Times",
"url": "images/2016/10/19/dining/19BAKEOFF2/19BAKEOFF2-articleLarge.jpg",
"rank": "1",
"height": 400,
"subtype": "large",
"legacy": [],
"type": "image",
"width": 600
}
],
"headline": {
"main": "‘The Great British Bake Off’ Changes the Way the British Bake",
"name": "Lemon Drizzle Cake"
},
"keywords": [],
"pub_date": "2016-10-19T00:00:00Z",
"document_type": "recipe",
"news_desk": null,
"section_name": null,
"subsection_name": null,
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Melissa",
"rank": 1,
"lastname": "Clark"
}
],
"original": "Melissa Clark"
},
"type_of_material": "Recipe",
"_id": "58066d7c253f0a7d028b07d0",
"word_count": "250",
"slideshow_credits": null
}
]
},
"status": "OK",
"copyright": "Copyright (c) 2013 The New York Times Company. All Rights Reserved."
}
Before start, I'm not familiar with retrofit.
If you want serialize/unserialize json to pojo, you better make pojo object.
class Response {
Meta meta;
List<Docs> docs;
}
class Docs {
String webUrl;
Mutimedia multimedia;
...
}
The issue the key getResponse(), from below model class, always gives me {} happens because gson cannot determine proper object to convert json to plain old java object.
In order to make your Response model class use http://www.jsonschema2pojo.org/.
Making POJO simple
you no need to make exact structure in model
you can go ahead with your code just need small change
Use
#SerializedName("response") private JsonObject response;
instead of
#SerializedName("response") private JSONObject response;
and in the import
import com.google.gson.JsonObject;

Categories

Resources