我有来自服务器的下一个响应 -
{"response":[{"uid":174952xxxx,"first_name":"xxxx","last_name":"xxx"}]}
我正在尝试以另一种方式反序列化它 -
JsonConvert.DeserializeObject<T>(json);
其中 t = VkUser 列表,但我得到错误。
[JsonObject]
public class VkUser
{
[JsonProperty("uid")]
public string UserId { get; set; }
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
}
我一直在尝试
public class SomeDto // maybe Response as class name will fix it but I don't want such name
{
public List<VkUser> Users {get;set;}
}
哪些反序列化选项可以帮助我?
使用 SelectToken:
string s = "{"response":[{"uid":174952,"first_name":"xxxx","last_name":"xxx"}]}";
var users = JObject.Parse(s).SelectToken("response").ToString();
var vkUsers = JsonConvert.DeserializeObject<List<VkUser>>(users);
正如Brian Rogers所指出的,你可以直接使用ToObject
:
var vkUsers = JObject.Parse(s).SelectToken("response").ToObject<List<VkUser>>();