我正在尝试使用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:+'
}
答案 0 :(得分:1)
试试这个:
Iterator < ? > keys = jObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if (jObject.get(key) instanceof JSONObject) {
}
}