无法解析android中的keySet()方法

时间:2016-12-28 14:33:57

标签: java android org.json

我正在尝试使用Android中的AsyncTask解析JSON数据。当我使用JSONObject类的keySet()方法时,它会抛出一个错误keySet() could not be resolved into a method。我在Eclipse中使用与Java项目相同的代码,并且工作正常。帮助

代码:

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 个答案:

答案 0 :(得分:1)

试试这个:

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

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