2014-03-30 58 views
0

我使用的是帶有newtonsoft.json的asp.net mvc4。我有服務器的響應,並嘗試去實現它,但有一個錯誤。底部的代碼和錯誤。有誰能夠幫助我?如何實現JObj Collection

{"response":[208212605,223947262]} 

var friends = JsonConvert.DeserializeObject<List<VkMutualFriends>>(response); 

[JsonObject] 
    public class VkMutualFriends { 
     [JsonProperty("response")] 
     public int UserId { get; set; } 
    } 



    Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 
    'System.Collections.Generic.List`1[SocialAnalytics.Models.ViewModel.VkMutualFriends]' 
    because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the 
deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, 
not a collection type like an array or List<T>) that can be deserialized from a JSON object. 
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 
     Path 'response', line 1, position 12. 
+0

這可以幫助你 - http://stackoverflow.com/questions/6657984/problem-deserialising-json-to-listt。 –

回答

1

該對象包含一個整數數組,但您正在轉換爲一個int。因此錯誤。

嘗試像

[JsonProperty("response")] 
public int[] UserIds { get; set; } 
+0

謝謝,它對我很好 – BorHunter