C# 如何反序列化JObj集合
我将asp.net mvc4与newtonsoft.json一起使用。我收到了来自服务器的响应,并尝试对其进行反序列化,但出现了一个错误。代码和错误在底部。有人能帮我吗C# 如何反序列化JObj集合,c#,asp.net-mvc-4,json.net,json,C#,Asp.net Mvc 4,Json.net,Json,我将asp.net mvc4与newtonsoft.json一起使用。我收到了来自服务器的响应,并尝试对其进行反序列化,但出现了一个错误。代码和错误在底部。有人能帮我吗 {"response":[208212605,223947262]} var friends = JsonConvert.DeserializeObject<List<VkMutualFriends>>(response); [JsonObject] public class VkMutual
{"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.
{“响应”:[208212605223947262]}
var friends=JsonConvert.DeserializeObject(响应);
[JsonObject]
公共类VKmualfriends{
[JsonProperty(“响应”)]
public int UserId{get;set;}
}
无法将当前JSON对象(例如{“name”:“value”})反序列化为类型
'System.Collections.Generic.List'1[SocialAnalytics.Models.ViewModel.VkMutualFriends]'
因为该类型需要一个JSON数组(例如[1,2,3])才能正确反序列化。
要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或者更改
反序列化类型,使其成为正常的.NET类型(例如,不是integer之类的基元类型,
不是可以从JSON对象反序列化的集合类型(如数组或列表)。
还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。
路径“响应”,第1行,位置12。
该对象包含一个整数数组,但您正在转换为单个整数。因此会出现错误
试试像这样的东西
[JsonProperty("response")]
public int[] UserIds { get; set; }
这可能会帮助你-。