【问题标题】:Parse Nested JSON data in Android在 Android 中解析嵌套的 JSON 数据
【发布时间】:2016-12-06 13:18:20
【问题描述】:

我正在尝试解析在 android 中看起来像这样的 JSON 数据;

{
"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"
                        }]
                }]
        }]
}]

}

当我通过列表视图列表项单击等选择特定节点时,我应该看到它的数据......

到目前为止,我已经设法解析所有类别并将它们显示在列表视图中,但我想显示我在列表视图中选择的类别的子类别。

【问题讨论】:

标签: android arrays json


【解决方案1】:

在您的列表视图上设置一个 onItemClickListener 并使用所选项目的位置返回相关的子类别?

例如,可能类似于以下内容:

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

【讨论】:

    【解决方案2】:

    使用此链接上给出的ExpandableListViewexample。 json解析使用谷歌gson,让你的工作轻松高效。

    【讨论】:

      【解决方案3】:

      似乎类似于How to parse this nested JSON array in android 但是你可以使用这个库https://github.com/FasterXML/jackson 使用 Objects 使工作变得更容易和更快。 这是一个小教程:JacksonInFiveMinutes 特别是:完整数据绑定 (POJO) 示例。和 JacksonDataBinding

      【讨论】:

        【解决方案4】:

        感谢大家的帮助。我终于设法解决了这个问题; 这是我的做法;

        JSONObject jsonObj = new JSONObject(jsonStr);
        JSONObject subcategories = jsonObj.getJSONObject("data").getJSONArray("categories").getJSONObject((int)getItemId(position));
        Log.e("TAG","Subcategories: " + subcategories);
        

        我不知道这是否是正确的方法,但它适用于我的情况。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-04
          • 2020-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多