Android:JSONArray无法转换为JSONObject
这是来自服务器的示例Android:JSONArray无法转换为JSONObject,android,arrays,json,Android,Arrays,Json,这是来自服务器的示例JSON数组 [ { "users":{ "uid":"5893048cc3a841.21437792", "name":"Jakob Nolan", "email":"jakob.nolan@gmail.com" } }, { "users":{ "uid":"589308eb640e79.86708812",
JSON数组
[
{
"users":{
"uid":"5893048cc3a841.21437792",
"name":"Jakob Nolan",
"email":"jakob.nolan@gmail.com"
}
},
{
"users":{
"uid":"589308eb640e79.86708812",
"name":"Donald Trump",
"email":"donaldtrump@usa.com"
}
}
]
我试图使用JSONArray
和JSONObject
来获取其中的值。所以我写了如下代码
String strResponse = response.body().string();
Log.i("Superman", strResponse);
try {
JSONObject jsonObject = new JSONObject(strResponse);
boolean error = jsonObject.getBoolean("error");
Log.i(TAG, ""+error);
if(!error) {
JSONArray people = jsonObject.getJSONArray("users");
JSONObject obj = people.getJSONObject(0);
final String name = obj.getString("name");
Log.i("TAG", name);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "it works! ", Toast.LENGTH_SHORT).show();
}
});
} else {
final String strError = jsonObject.getString("error_msg");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), strError, Toast.LENGTH_SHORT).show();
}
});
hideDialog();
}
} catch (final JSONException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "JSONException caught: " + e.getMessage());
}
});
我运行了上面的代码,得到了如下JSONException
02-03 17:22:14.091 27684-27684/com.marshall.authentication I/MainActivity: JSONException caught: Value [{"users":{"uid":"5893048cc3a841.21437792","name":"Jakob Nolan","email":"jakob.nolan@gmail.com"}},{"users":{"uid":"589308eb640e79.86708812","name":"Donald Trump","email":"donaldtrump@usa.com"}}] of type org.json.JSONArray cannot be converted to JSONObject
从JSON数组中解析JSON对象时,我是否做错了什么?您的响应是JSONArray
,但您将其解析为JSONObject
。按以下步骤进行:
JSONArray responseArray = new JSONArray(strResponse);
for(int i =0; i<responseArray.length(); i++){
JSONObject eachObject = responseArray.getJSONObject(i);
//do other parsing in each object
}
JSONArray responseArray=新的JSONArray(strResponse);
对于(int i=0;i您接收的JSON无效。至少对于给定的情况无效。您现在有一个未命名的数组,其中包含命名的对象。要有一个包含用户的正确数组,它应该如下所示:
{
"users": [
{
"uid":"5893048cc3a841.21437792",
"name":"Jakob Nolan",
"email":"jakob.nolan@gmail.com"
},
{
"uid":"589308eb640e79.86708812",
"name":"Donald Trump",
"email":"donaldtrump@usa.com"
}
]
}
另外,您现在得到的响应是一个JSONArray
,因为它是用[]
包装的。因此,在这种情况下,您应该使用JSONArray
而不是JSONObject
。我喜欢将JSON包装在{}
中,以便始终可以将其作为对象进行解析,并从中获取任何数组,就像本例中的urers
您的响应不是json对象,而是json数组。该数组中的每个项都是一个名为users
的对象。在logcat
中,您的响应返回JSONArray
。
实际上,您要做的是将JSONArray
转换为JSONObject
JSONObject jsonObject = new JSONObject(strResponse);
这是类文件代码
尝试将此更改为
JSONArray jsonArray = new JSONArray(strResponse);
为了进一步从上面获取JSONObject,您可以解析这个JSONArray
像
如果您的JSON中没有布尔值
类型值。对解决方案有什么建议吗?如果您无法更改JSON,那么我认为您可能可以通过d1vivek681065的答案实现您想要的。但是您收到的JSON,尽管在语法上是有效的,但看起来不像是正常的用户数组。@MarshallS.Lee请参阅我的a回答
JSONArray jsonArray = new JSONArray(strResponse);
for(int i =0; i<jsonArray .length(); i++){
JSONObject jsonObject= jsonArray.getJSONObject(i);
JSONArray people = jsonObject.getJSONArray("users");
String name = people.getString("name");
//do other parsing in each string
}
boolean error = jsonObject.getBoolean("error");