【发布时间】:2013-03-17 16:37:27
【问题描述】:
我有一个来自这样的 API 服务的响应字符串:
{"id":"login","status":"true"}
这是解析响应字符串以从键“状态”获取值的方法
JSONObject jsonObj = null;
try{
jsonObj = new JSONObject(responseString);
}
catch(JSONException e){
e.printStackTrace();
}
JSONArray innerJsonArray = null;
try {
innerJsonArray = jsonObj.getJSONArray("status");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject = null;
try {
jsonObject = innerJsonArray.getJSONObject(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(jsonObject.getString("status"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我有错误 "org.json.JSONArray 无法转换为 JSONObject"
谁能给我建议?
【问题讨论】:
-
使用
getBoolean()而不是getJSONArray()... -
显然错误指出您正在尝试将 Jsonobject 转换为 JsonArray 。因为当前字符串仅包含 jsonobject
标签: java android arrays json response