2013-12-13 66 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