看起来像一个简单的问题,我知道已经有这样的问题。我正在尝试从数组中的数组中获取JSON数据。
我有一个JSON数组如下:
{
"results": [
{
"name": "John Doe",
"time": "09:00:00",
"claim": {
"date": "2015-08-06",
"comments": "Some comments"
}
},
{
"name": "Mary Smith",
"time": "10:00:00",
"claim": {
"date": "2015-08-06",
"comments": "Some comments"
}
}
}
]
}
我正在尝试解析其中的所有信息,我能够获得name
和time
,但我无法从{{1}获取数据数组。
我已经在SO上查看了多个类似的问题,但他们的解决方案都没有解决我的问题。
这是我的Java代码:
claim
我收到了这个错误:
@Override
protected void onPostExecute(JSONObject json) {
if (dialog.isShowing()) {
dialog.dismiss();
}
try {
// Getting JSON Array from URL
JSONArray android = json.getJSONArray(TAG_RESULTS);
for (int i = 0; i < android.length(); i++) {
JSONObject c = android.getJSONObject(i);
// Storing JSON items in Variables
String time = c.getString(TAG_TIME);
//For loop for claims array:
JSONArray claims = c.getJSONArray(TAG_CLAIM);
for (int j = 0; j < claims.length(); j++) {
JSONObject d = claims.getJSONObject(j);
String name = d.getString(TAG_NAME);
//other parsing and list view.
在这一行:org.json.JSONException: Value {"date": "2015-08-06","comments": "Some comments"} at claim of type org.json.JSONObject cannot be converted to JSONArray
08-06 12:43:27.436 17586-17586/com.murrion.navigationdrawer W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
08-06 12:43:27.436 17586-17586/com.murrion.navigationdrawer W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:588)
我不确定出了什么问题,我非常感谢你的帮助。感谢
答案 0 :(得分:3)
claim
不是数组,而是另一个对象。如果你想让它成为一个数组,它应该是这样的:
"claim": [
"date": "2015-08-06",
"comments": "Some comments"
]
(请注意[
]
而不是{
}
)
如果它是一个对象,则代码应为:
JSONObject claims = c.getJSONObject(TAG_CLAIM);
答案 1 :(得分:1)
试试这个
{
"results": [
{
"name": "John Doe",
"time": "09:00:00",
"claim": [
"date": "2015-08-06",
"comments": "Some comments"
]
},
{
"name": "Mary Smith",
"time": "10:00:00",
"claim": [
"date": "2015-08-06",
"comments": "Some comments"
]
}
]
}
注意:错误堆栈跟踪中明确提到了json中的问题,它告诉您正在尝试解析json对象(在{...}内)作为json数组(数组应该在[...]内,而不是{...})。所以请尝试阅读堆栈跟踪。
答案 2 :(得分:0)
删除Extra}花括号后,你的json就像这样
{
"results" :
[
{
"name" : "John Doe",
"time" : "09:00:00",
"claim" :
{
"date" : "2015-08-06",
"comments" : "Some comments"
}
},
{
"name" : "Mary Smith",
"time" : "10:00:00",
"claim" :
{
"date" : "2015-08-06",
"comments" : "Some comments"
}
}
]
}
and to parse this json your code like this
@Override
protected void onPostExecute(JSONObject json)
{
if (dialog.isShowing()) {
dialog.dismiss();
}
try
{
// Getting JSON Array from URL
JSONArray android = json.getJSONArray(TAG_RESULTS);
for (int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON items in Variables
String time = c.getString(TAG_TIME);
String time = c.getString("name");
// for claims object:
JSONObject claims = c.getJSONObject(TAG_CLAIM);
String date= claims .getString("date");
String comments= claims .getString("comments");
//other parsing and list view.
}
}
catch(Exception e){}
}
正如你所说,你的json应该是这样的
{
"results" :
[{
"name" : "John Doe",
"time" : "09:00:00",
"claim" : [{
"date" : "2015-08-06",
"comments" : "Some comments"
}
]
}, {
"name" : "Mary Smith",
"time" : "10:00:00",
"claim" : [{
"date" : "2015-08-06",
"comments" : "Some comments"
}
]
}
]
}
和你的代码一样
@Override
protected void onPostExecute(JSONObject json)
{
if (dialog.isShowing()) {
dialog.dismiss();
}
try
{
// Getting JSON Array from URL
JSONArray android = json.getJSONArray(TAG_RESULTS);
for (int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON items in Variables
String time = c.getString(TAG_TIME);
String time = c.getString("name");
// for claims object:
JSONArray claims = c.getJSONArray(TAG_CLAIM);
for (int j = 0; j < claims.length(); j++) {
JSONObject d = claims.getJSONObject(j);
String date= d.getString("date");
String comments= d.getString("comments");
//other parsing and list view.
}
}
}
catch(Exception e){}
}