Reputation: 80
my app consumes data from rest api and gets json
code in my app:
var content = await httpClient.GetStringAsync("http://192.168.0.101:8000/api/list");
JSON
"count": 10,
"next": "http://127.0.0.1:8000/api/list?page=2",
"previous": null,
"results": [
{
"name": "lorem ipsum1",
"image": "http://127.0.0.1:8000/media/pictures/2.jpg",
"description": "lorem ipsum5"
},
{
"name": "lorem2",
"image": "http://127.0.0.1:8000/media/pictures/1.jpg",
"description": "lorem ipsum4"
}
and i have a class i want to use to deserialize "count"
public class Count
{
public int count { get; set; }
}
my question is: can i deserialize only "count": 10 ? Thanks in advance
Upvotes: 1
Views: 156
Reputation: 89082
if you only want a single value, you can just parse the data into a JObject
var data = JObject.Parse(json);
Console.WriteLine(data["count"]);
Upvotes: 0
Reputation: 5737
Absolutely. You just have to build a model class with that single property Count
and deserialize with Newtonsoft.Json.
public class Dto
{
public int Count { get; set; }
}
I made a .NET fiddle for you:
Note that I had to fix your sample JSON:
.Replace("'", "\"");
is just required because I had to use '
instead of "
in the C# string. You don't need that.Upvotes: 1