tony tony - 1 year ago 134
JSON Question

How do I deserialize a JSON array and ignore the root node?

I have next response from server -


I am trying to deserialize this in next way -


Where T = List of VkUser, but I got error.

public class VkUser
public string UserId { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

I always tryed

public class SomeDto // maybe Response as class name will fix it but I don't want such name
public List<VkUser> Users {get;set;}

What deserialization options can help me? Thanks.

Answer Source

Use 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);

as pointed out by Brian Rogers, you can use ToObject directly:

var vkUsers = JObject.Parse(s).SelectToken("response").ToObject<List<VkUser>>();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download