0

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.

Cybermatatu
  • 345
  • 2
  • 7
  • 2
    hava a look at [jsonschema2pojo](http://www.jsonschema2pojo.org/) and [Google Gson](https://github.com/google/gson) this will make it a loot easier – Simon Schnell Dec 06 '16 at 13:22

4 Answers4

1

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;
                   ....
                }
            }
0

Use ExpandableListView, example given on this link. For json parsing use Google gson, it will make your work easy and efficient.

Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
0

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

Community
  • 1
  • 1
BillyGL
  • 83
  • 2
  • 9
0

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.

Cybermatatu
  • 345
  • 2
  • 7