Cannot resolve keySet() method in android

3.6k views Asked by At

I am trying to parse JSON data using AsyncTask in android. When I use the keySet() method of the JSONObject class, it is throwing an error saying keySet() could not be resolved into a method. I am using the same code in Eclipse for a Java project and that is working fine. Help.

Code:

JSONObject data=(JSONObject) new JSONTokener(IOUtils.toString(new URL(strings[0]))).nextValue();
JSONObject pages=data.getJSONObject("query").getJSONObject("pages");
for(String key:pages.keySet()){
result=pages.getJSONObject(key).getString("extract");
}

build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:support-v4:24.2.0'
    compile 'org.json:json:20160810'
    compile 'commons-io:commons-io:+'
}
1

There are 1 answers

1
Ricardo Romero Benítez On BEST ANSWER

Try this:

Iterator < ? > keys = jObject.keys();

while (keys.hasNext()) {
    String key = (String) keys.next();
    if (jObject.get(key) instanceof JSONObject) {
    }
}