Java 无法将org.json.JSONObject类型的数据转换为JSONArray
我不明白,为什么我会有这个错误: 04-24 22:11:51.263:W/System.err(27504):org.json.JSONException:Value 无法删除org.json.JSONObject类型的数据处的Java 无法将org.json.JSONObject类型的数据转换为JSONArray,java,android,json,Java,Android,Json,我不明白,为什么我会有这个错误: 04-24 22:11:51.263:W/System.err(27504):org.json.JSONException:Value 无法删除org.json.JSONObject类型的数据处的 转换为JSONArray 这是我的代码: JSONObject getProgile = null; try { //get json getProgile = new JSONObject(CustomHttpClient.executeHtt
转换为JSONArray
这是我的代码:
JSONObject getProgile = null;
try {
//get json
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
//convert array
JSONArray array = getProgile.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
//get TAG_CUSTOMER
JSONObject customer = c.getJSONObject("Customer");
pName = customer.getString("name");
pLname = customer.getString("name");
}
问题是,您正在尝试使用JSON数组转换JSON对象,因此将发生错误 如果您只想获取名称,则可以执行以下操作:
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
JSONObject obj1 = getProgile.getJSONObject("data");
JSONObject obj2 = array.getJSONObject("Customer");
String name = obj2.getString("name");
JSON显示数据是JSONObject而不是JSONArray,因此调用getProfile.getJSONObject(“数据”)web请求的返回值不是JSON数组。打印试图将JSONObject转换为JSONArray的
CustomHttpClient.executeHttpGet的结果。这根本不可能。请在此处记录GetProFile对象。键入“data”
的值是{“Customer”:…}
。这是一个JSON对象。您正试图将其投射到阵列。这相当于Map m=whatever();对象o=m;List l=(List)o
,这也将抛出ClassCastException。@Jaec您遇到了什么<代码>我有JSONobject->conver JSONarry->JSONobject c=array.getJSONObject(I)->JSONobject customer=c.getJSONObject(“客户”)
这是可能的
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
JSONObject obj1 = getProgile.getJSONObject("data");
JSONObject obj2 = array.getJSONObject("Customer");
String name = obj2.getString("name");