2016-12-06 158 views
0

我想解析看起来像这样在android中的JSON数据;解析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" 
         }] 
       }] 
     }] 
}] 

}

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

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

+2

hava看看[jsonschema2pojo](http://www.jsonschema2pojo.org/)和[Google Gson](https://github.com/google/gson)这会让它变得更容易 –

回答

0

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

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

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

0

使用ExpandableListView,example在此链接上给出。 对于json解析使用Google gson,它会让你的工作变得简单而高效。

0

它看起来类似于How to parse this nested JSON array in android 但是,您可以使用此库来使用对象使工作变得简单快捷。 这是一个小教程:JacksonInFiveMinutes 特别是:完整数据绑定(POJO)示例。和JacksonDataBinding

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