Casey Crookston Casey Crookston - 2 years ago 195
C# Question

remove pre-pending "data" from json string

I am making a call to a third party API, and the json string I get back looks like this:

{
"data":[
{"id":197567,"name":"101","url":"http://www.foobar1.com"},
{"id":197568,"name":"102","url":"http://www.foobar2.com"},
.....
{"id":197569,"name":"120","url":"http://www.foobar20.com"}
],
"offset":0,
"pageSize":2,
"count":20
}


And then I have a class
FoorBarURIs
:

private class FoorBarURLs
{
public int id { get; set; }
public string name { get; set; }
public string url { get; set; }
}


So, I don't care about the
offset
or the
pagesize
or the
count
. All I need is the data inside
data
.

Given that what I'm starting with is just a string, how do I extract that list from
data
so I can do this:

List<FoorBarURLs> myList = JsonConvert.DeserializeObject<List<FoorBarURLs>>(???)

Answer Source

Use a temporary JObject

var obj = JObject.Parse(json)["data"].ToObject<List<FoorBarURLs>>();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download