Newtonsoft.Json: How can I map specific properties?

Imagine I have a class like this one:

public class Foo
{
    [JSonProperty("a")]
    public int a;

    [JSonProperty("b")]
    public int b;

    public List<Foo> foos;
}

and imagine I have a Json like this one:

{
"a": "0",
"b": "1",
"moreFoos": {
    "total" : "2",
    "foos" : [
        {
            "a" : "2",
            "b" : "3"
        }, 
        {
            "a" : "4",
            "b" : "5"
        }
    ]
}
}

So, what I want to do is to deserialize with JsonConvert.DeserializeObject(Foo) all properties, but right now only “a” and “b” are deserialized. I have tried to put something like this on foos property:

[JsonProperty("moreFoos.foos")]
public List<Foo> foos;

but it does not work, foos is null. Do you know if there is a way to map properties dynamically this way? Of course, I would like to avoid creating a new class with an int property called “total” and another one called foos as a List of Foo objects.

Regards,
Román.


Source: json

Leave a Reply