2016-12-28 67 views
-1

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

代码:

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

'pages.keySet()'应该是'pages.keys()' – Blackbelt

+0

这个JSONObject的包装是什么? 'org.json'? –

+0

这个JSONObject的包装是什么? 'org.json'? –

回答

1

试试这个:

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

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

完美无瑕的兄弟。谢谢 –