How to get the list of all tag names of json array

2019-02-20 16:29发布

I have a small problem parsing json response because it constantly keeps on getting updated whenever i send a request.All the examples I have seen makes us provide the tag name. My question is that I am trying to parse data from a request sent through an API and I want to list out all the tags of all JSON Arrays existing within a JSON Object before I start parsing. Is it possible in android. http://api.yamgo.tv/channel?apiKey=187abeefc53f900600dc0fc5b8f913a0&token=892e069fa48eead5e7f84cddafe7f0ba This is the request I am sending and it gives me a json response. which has channels as a json object and within it many json arrays with tags like bollywood, entertainment, music,etc. Use an online parser for the response to check it out. I want this list of array names. How can I do it. Could anyone please help. Thanks.

2条回答
再贱就再见
2楼-- · 2019-02-20 17:08

Where jsonObj is the JSONObject which contains some data, By these following code you can get only field names, if u want for column values then you can add further for value for keys.

List<String> listArray = new ArrayList<String>();
    Iterator iter = jsonObj.keys();
    int count=0;
    while(iter.hasNext()){
        listArray.add((String)iter.next());
        count +=1;
    }
    System.out.println(listArray);

output: [projector, video_conference, polycom, lcd, digital_phone, speaker_phone]
查看更多
甜甜的少女心
3楼-- · 2019-02-20 17:14

I actually figured it out with a small help from one of my friend. Needed the use of Iterator object. Here is the snippet of code

JSONObject channels = jobj.getJSONObject(TAG_CHANNELS);
    Iterator<?> keys=channels.keys();
    while( keys.hasNext() )
    {
        String key = (String)keys.next();
        Log.e("Key", key);
        if( channels.get(key) instanceof JSONArray ){
            jsontags.add(key);
        }
    }
查看更多
登录 后发表回答