从json响应中读取主题列表值

时间:2018-09-10 04:53:07

标签: android json arraylist retrofit

看下面

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "customMetaTags": [
                {
                    "metatag": "#TAPE"
                }
            ],
            "geoPolitical": [
                {
                    "gp_code": "Default  / All GPIDs"
                }
            ],
            "entity": [],
            "topics": [
                {
                    "topic_code": "#CAR"
                },
                {
                    "topic_code": "#CAREER"
                },
                {
                    "topic_code": "#CAREERS"
                },
                {
                    "topic_code": "#CARE"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

请尝试这个

try {
                ArrayList<String> topicsList = new ArrayList<>();
                JSONObject jsonObject = new JSONObject(resultJSON);
                JSONArray result = jsonObject.getJSONArray("results");
                for(int i=0;i<result.length();i++) {
                    JSONObject jsonObject1 = result.getJSONObject(i);
                    JSONArray topics = jsonObject1.getJSONArray("topics");
                    for(int j=0;j<topics.length();j++) {
                        JSONObject jsonObject11 = topics.getJSONObject(j);
                        topicsList.add(jsonObject11.getString("topic_code"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }