I am trying to parse JSON data which looks like this in android;
{
"data": [{
"http_code": 200,
"message": "success",
"created_at": "2016/10/12",
"categories": [{
"cat_id": 1,
"cat_name": "Business and Commerce",
"subcategories":[{
"subcat_id": 1,
"subcat_name": "Intellectual Property",
"articles":[{
"article_id": 1,
"article_heading": "What is intellectual?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of intellectual Properties",
"article_url": "http://example.com/2"
}]
}, {
"subcat_id": 2,
"subcat_name": "Business Licensing",
"articles":[{
"article_id": 1,
"article_heading": "What is a license?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of Business Licenses",
"article_url": "http://example.com/2"
}]
}]
}, {
"cat_id": 2,
"cat_name": "Family Law",
"subcategories":[{
"subcat_id": 3,
"subcat_name": "Domestic Violence",
"articles":[{
"article_id": 1,
"article_heading": "What is domestic violene?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of domestic violence",
"article_url": "http://example.com/2"
}]
}, {
"subcat_id": 4,
"subcat_name": "Marriage",
"articles":[{
"article_id": 1,
"article_heading": "What is a marriage?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of marriages",
"article_url": "http://example.com/2"
}]
}]
}]
}]
}
I am supposed to see data for a specific node when I select it via a listview list item click and so on....
So far I have managed to parse all the categories and display them in a listview, but I want to display subcategories for a category I select on the listview.
Set an onItemClickListener on your listview and use the position of the selected item to return the relevant subcategories?
For example, perhaps something along the lines of:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Switch(position) {
case 0:
//get first subcategories array values
break;
case 1:
//get second subcategories array values
break;
....
}
}
Use ExpandableListView, example given on this link.
For json parsing use Google gson, it will make your work easy and efficient.
It seems similar to How to parse this nested JSON array in android
But you can use this lib https://github.com/FasterXML/jackson
to make the work easy and faster using Objects.
It is a small tutorial: JacksonInFiveMinutes
Specially: Full Data Binding (POJO) Example. And JacksonDataBinding
Thanks everyone for your help. I finally managed to solve the issue;
Here is how I did it;
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject subcategories = jsonObj.getJSONObject("data").getJSONArray("categories").getJSONObject((int)getItemId(position));
Log.e("TAG","Subcategories: " + subcategories);
I don't know if that is the correct way, but it worked in my case.
Related
This question already has answers here:
How to parse JSON in Kotlin?
(17 answers)
Closed 3 years ago.
I have a response json like this,
[{
"userId": 1,
"id": 1,
"title": "quidem molestiae enim"
},
{
"userId": 2,
"id": 2,
"title": "sunt qui excepturi placeat culpa"
},
{
"userId": 3,
"id": 3,
"title": "omnis laborum odio"
}]
I want to show detail from userId = 3, I wasn't provided an endpoint to get detail. So I want to get detail from this endpoint.
in App:
I've shown the data for dashboard (using recyclerview/list)
After that, I want to go to detail from one data that I selected.
Case: I'm using 1 endpoint (like above)
JSONArray jsonArray = (JSONArray) JSON.parse(str);
Map map = jsonArray.stream().map( obj -> {JSONObject jsonobject = (JSONObject) obj;return jsonobject;}).collect(Collectors.toMap(obj->obj.getIntValue("userId"),obj->obj.toJSONString()));
System.out.println(map.get(3));
the start point of json must always be a jsonObject not jsonArray. head to your backend developer and ask him or her to put your jsonArray inside a jsonObject.
{
"docs":[{
"userId": 1,
"id": 1,
"title": "quidem molestiae enim"
},
{
"userId": 2,
"id": 2,
"title": "sunt qui excepturi placeat culpa"
},
{
"userId": 3,
"id": 3,
"title": "omnis laborum odio"
}]
}
I'm trying to use the New york times api to show the headline and an image in a listview on android. I'm already getting the data for headline and date and outputting to log cat. The real problem is: whenever I put the extra field to get the mulimedia (images url) the "GC_FOR_ALLOC freed 1902K (64), 22% free 11931K/15156K" is shown
I've seen that the api always sends fixed number of 10 nyt articles, but each of the 10 has a multiple multimedia urls and json with arrays nested and the app freezes. I'll learn to put the data on listview on my own, my real question is, whats the best approach to fix the crash, I only want 1 url image per article.
//THE RETURNED JSON WITH ONLY "MULTIMEDIA" FIELD QUERY.
{
"status": "OK",
"copyright": "Copyright (c) 2018 The New York Times Company. All Rights
Reserved.",
"response": {
"docs": [
{
"multimedia": [
{
"rank": 0,
"subtype": "xlarge",
"caption": null,
"credit": null,
"type": "image",
"url": "images/2018/09/07/opinion/07Parcak/merlin_143215881_20cecc31-7c8d-4b8f-8541-773170c1822c-articleLarge.jpg",
"height": 400,
//MORE CODE HERE.. THEN NEXT ARRAY BELOW
{
"multimedia": [
{
"rank": 0,
"subtype": "xlarge",
"caption": null,
"credit": null,
"type": "image",
"url": "images/2018/09/03/world/03xp-brazil-promo/03xp-brazil-promo-articleLarge.jpg",
"height": 400,
"width": 600,
"legacy": {
"xlarge": "images/2018/09/03/world/03xp-brazil-promo/03xp-brazil-promo-articleLarge.jpg",
"xlargewidth": 600,
"xlargeheight": 400
},
"subType": "xlarge",
"crop_name": "articleLarge"
},
// Goes on forever.
What do you use to get the data, what kind of REST client are you using?
For example if you are parsing the response by yourself you can put some logic like
JSONObject obj = new JSONObject(" .... ");
JSONArray arr = obj.getJSONArray("multimedia");
JSONObject firstMultimedia = null;
if (arr.length() > 0){
firstMultimedia = arr.getJSONObject(0);
}
I have a calendar like this:
materialise calendar
every tic icon shows their corresponding event.
and I have a json response like this:
{
"Table": [
{
"userid": 4,
"eventname": "adi",
"eventdate": "/Date(1484121600000-0800)/",
"eventcolor": "2413AD",
"autoid": 2005
},
{
"userid": 4,
"eventname": "Aditya",
"eventdate": "/Date(1479974400000-0800)/",
"eventcolor": "5986FF",
"autoid": 1011
},
{
"userid": 4,
"eventname": "aditya",
"eventdate": "/Date(1484812800000-0800)/",
"eventcolor": "13AD1A",
"autoid": 2006
},
{
"userid": 4,
"eventname": "dfgdgdgs",
"eventdate": "/Date(1478678400000-0800)/",
"eventcolor": "AD3D1F",
"autoid": 1005
},
{
"userid": 4,
"eventname": "dfgdgdgs",
"eventdate": "/Date(1478678400000-0800)/",
"eventcolor": "AD3D1F",
"autoid": 1006
}
]
}
Here comes the real problem.When I click any date on the calendar, above json response comes in a listView according to sequence of json. But, I want to change the sequence according to which date i clicked(i.e. Date i am clicked in the calendar, that date comes first in the list and remaining item comes after that).
I'm not posting any code, If any one wants to see my code, please ask.
When you parse the list from JSON at that time you check the date of each object and compare to the clicked date and store that particular index.
Then you simply parse the list from JSON response
at parsing time you remove that particular object at that stored index and move to the first position of that list.
Use code like:
list.removeAt(stored_index);
list.add(0, objectOfList);
and then simply add that list to adapter
The problem with the following JSON response parsing is that is giving the error as "Unterminated object at character 32".
{
"0": {
"review": {
"reviewTime": "2015-09-24 22:07:03",
"author": "John Doe",
"rating_5": 1.5,
"rating": 2
}
},
"1": {
"review": {
"reviewTime": "2015-09-25 18:05:14",
"author": "Samantha",
"rating_5": 5,
"timestamp": 1443184514,
"rating": 5
}
},
"count": 3,
"review_url": "https://localhost/reviews"
}
I'm validated your json using in http://jsonformatter.curiousconcept.com/ and it's valid.
The 32 character is a date and must be quoted. Check in debug mode if the date is quoted.
In one of your comments to your question you wrote that your response looks like:
{ count=2041.0, 4={review={reviewTime=2015-09-24 22:07:03, author=Neha Primith, rating_5=1.5, rating=2.0, reviewTimeFriendly=yesterday}}, 1={review={reviewTime=2015-09-24 22:07:03, author= John Doe, rating_5=1.5, rating=2.0,}}, 0={review={reviewTime=2015-09-25 18:05:14, author=Samantha, rating_5=5.0, rating=5.0}}, , review_url=https://localhost/reviews,count=2 }
But it's not json format. Json format is sensitive to quotes. So make sure you get a properly formatted response
I am implementing the Bing search for image searching.
I refered this link, and change my response to JSON.
It works and the result is like:
{
"d": {
"results": [{
"__metadata": {
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query=\u0027Pepsi\u0027&$skip=0&$top=1",
"type": "ImageResult"
},
"ID": "64737694-fc53-4d68-933d-10edc214fd3a",
"Title": "Pepsi: Like Madonna, its look has been reinvented time and time again ...",
"MediaUrl": "http://tjthesportsgeek.files.wordpress.com/2012/02/pepsi.png",
"SourceUrl": "http://tjthesportsgeek.com/2012/02/14/tale-of-the-tape-coke-vs-pepsi/",
"DisplayUrl": "tjthesportsgeek.com/2012/02/14/tale-of-the-tape-coke-vs-pepsi",
"Width": "1588",
"Height": "2064",
"FileSize": "569827",
"ContentType": "image/png",
"Thumbnail": {
"__metadata": {
"type": "Bing.Thumbnail"
},
"MediaUrl": "http://ts2.mm.bing.net/th?id=OIP.M7f9b9a39639b9ca8bb6f1cba6e35d041H0&pid=15.1",
"ContentType": "image/jpg",
"Width": "369",
"Height": "480",
"FileSize": "19879"
}
},
// Next array element
]
}
}
but what i need is:
The json result with only MediaUrl and MediaUrl of Thumbnail. (As it is possible in Google Custom Search with the help of tag "field" like fields=items(link,image/thumbnailLink))
The image only with medium size. (I searched for this and applied the filter but of no use.)
Please reply with your valuable suggestions.
You can't filter output fields.
Use the parameter: ImageFilters = Size.Medium
My ref: https://onedrive.live.com/view.aspx?resid=9C9479871FBFA822!109&app=Word&authkey=!ACvyZ_MNtngQyCU