我有以下由web api返回的json,但是在嘗試解析它時出現以下錯誤:這不是json數組。這不是一個json數組 - json被web api返回
{
"$id": "1",
"updateTime": "2013-12-10T12:28:26.6533786+00:00",
"user": {
"$id": "2",
"CompanyID": 1,
"username": "test",
"Firstname": "test",
"Lastname": "ing"
},
"Vehicles": {
"$id": "3",
"$values": [
{
"$id": "4",
"VehicleID": 1,
"CompanyID": 1,
"VehicleReg": "123",
"VehicleTypeID": 1,
"VehicleDescription": ""
},
{
"$id": "5",
"VehicleID": 4,
"CompanyID": 1,
"VehicleReg": "456",
"VehicleTypeID": 2,
"VehicleDescription": ""
},
{
"$id": "6",
"VehicleID": 7,
"CompanyID": 1,
"VehicleReg": "789",
"VehicleTypeID": 3,
"VehicleDescription": ""
}
]
},
"VehicleTypes": {
"$id": "7",
"$values": [
{
"$id": "8",
"VehicleTypeID": 1,
"VehicleType": "First Test Type",
"CompanyID": 1
},
{
"$id": "9",
"VehicleTypeID": 2,
"VehicleType": "Second Test Type",
"CompanyID": 1
},
{
"$id": "10",
"VehicleTypeID": 3,
"VehicleType": "Third Test Type",
"CompanyID": 1
}
]
},
"Questions": {
"$id": "11",
"$values": [
{
"$id": "12",
"QuestionID": 1,
"SectionID": 1,
"CompanyID": 1,
"Question": "Question 1"
},
{
"$id": "13",
"QuestionID": 2,
"SectionID": 1,
"CompanyID": 1,
"Question": "Question 2"
},
{
"$id": "14",
"QuestionID": 3,
"SectionID": 1,
"CompanyID": 1,
"Question": "Question 3"
}
]
},
"QuestionSections": {
"$id": "15",
"$values": [
{
"$id": "16",
"SectionID": 1,
"CompanyID": 1,
"SectionName": "Section 1"
}
]
}
}
我已經通過解析器把這個和它說這是有效的JSON。
我試圖解析汽車在JSON像這樣:
Gson gsonv = new Gson();
JsonParser parser = new JsonParser();
JsonArray Jarray = (JsonArray)parser.parse(reader).getAsJsonArray();
ArrayList<VehicleEntity> lcs = new ArrayList<VehicleEntity>();
for(JsonElement obj : Jarray)
{
VehicleEntity v = gsonv.fromJson(obj , VehicleEntity.class);
lcs.add(v);
Log.d(TAG, "Vehicles: " + v.VehicleID);
}
我VehicleEntity屬性在JSON車輛部分匹配。
我在這裏正確的道路上,或者它是由Web API返回的JSON的問題?
由於 保羅
我從來沒有使用GSON所以可能是我錯了,但你有第一個點頭是不是一個JSON數組,實際上沒有根元素是jsonarrays –