JSONException:无法将值转换为JSONObject

时间:2016-07-12 20:01:01

标签: php android json android-asynctask dialog

当编译器到达onPostExecute并尝试运行执行JSONArray

# User-specific stuff:
*/.idea/workspace.xml
*/.idea/tasks.xml
*/.idea/dictionaries
*/.idea/vcs.xml
*/.idea/jsLibraryMappings.xml

# Sensitive or high-churn files:
*/.idea/dataSources.ids
*/.idea/dataSources.xml
*/.idea/dataSources.local.xml
*/.idea/sqlDataSources.xml
*/.idea/dynamic.xml
*/.idea/uiDesigner.xml

#MAC OS
.DS_Store

#Intellij
*/target/
*/out/
*.iml
*/.idea/


#Properties
*/*.properties

#IntelliJ
*/out/
*/target/



## File-based project format:
*.iws

## Plugin-specific files:


# mpeltonen/sbt-idea plugin
.idea_modules/

抛出异常:

jsonArray=jsonObject.getJSONArray("server_response");

什么是正确的陈述?

我的代码:

org.json.JSONException: 
    Value[{"code":"login_true","name":"hhh","email":"hhh"}]
    of type org.json.JSONArray cannot be converted to JSONObject".

1 个答案:

答案 0 :(得分:1)

假设从服务器返回的JSON是:

[{"code":"login_true","name":"hhh","email":"hhh"}]

解析上述JSON

try {
  JSONArray jsonArray = new JSONArray(json);
  JSONObject jsonObject = jsonArray.getJSONObject(0);
  String code = jsonObject.getString("code");
  String name = jsonObject.getString("name");
} catch (JSONException e) {
  e.printStackTrace();
}