我使用asp.net mvc4和newtonsoft.json。我有来自服务器的响应,并试图去实现它,但有一个错误。代码和错误在底部。有人能帮助我吗?
{"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 :(得分:1)
该对象包含一个整数数组,但您要转换为单个int。因此错误。
尝试类似
的内容[JsonProperty("response")]
public int[] UserIds { get; set; }