How to get the list of all tag names of json arrays within a json object in android/java

前端 未结 2 991
日久生厌
日久生厌 2021-01-16 03:44

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 n

相关标签:
2条回答
  • 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);
            }
        }
    
    0 讨论(0)
  • 2021-01-16 04:05

    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]
    
    0 讨论(0)
提交回复
热议问题