2013-12-13 26 views
0

我有以下由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的问题?

由于 保罗

+0

我从来没有使用GSON所以可能是我错了,但你有第一个点头是不是一个JSON数组,实际上没有根元素是jsonarrays –

回答

1

顶层JSON是一个JSONObject(由{}包围,键)不是一个JSONArray(由[],基本上是一个列表包围)。

这听起来像你期待这些对象的数组,但它看起来不像你得到的那样。

如果你在'['和']'中包装这个对象,你会得到一个长度为1的数组,也许你的代码可以工作。

如果只有1个元素,有时服务器设置会发送单个对象而不是JSONArray。也许是这样的?如果您执行返回多个结果的服务器查询,该怎么办?那么你的代码工作?

+0

看着JSON有3辆车我只是不是期待$ ids $ values:tags,也许这就是问题所在?我能否指出解析器让我回到车辆/值?因为在这之后,对象被[]包围。暂时不要在电脑上测试,但你认为这会起作用吗?谢谢 – paul

+0

是的,“$ values”是一个有效的JSONArray ...如果你问的是如何从顶级JSONObject获取车辆数组,它可能是这样的(未经测试): Gson gsonv = new Gson ); JsonParser parser = new JsonParser(); JsonObject topJsonObject =(JsonArray)parser.parse(reader).getAsJsonObject(); JSONArray jsonArray = topJsonObject.getJsonObject(“Vehicles”)。getJsonArray(“$ values”); – Matt

+0

是啊,试过,但编译器不喜欢:JsonObject topJsonObject =(JsonArray)parser.parse(reader).getAsJsonObject();无法从JsonArray转换为JsonObject。 – paul