Vladislav Kasianenko Vladislav Kasianenko - 2 months ago 18
C# Question

JsonConvert.DeserializeObject with folding

How can I specify folding?

Here's my json:

{
"result":
{
"code": "123",
"version": "1.2.3"
},
"error": null
}


And here's my class I want to deserialize:

public class Info
{
[JsonProperty("code")]
public string Code { get; set; }

[JsonProperty("version")]
public string Version { get; set; }

[JsonProperty("error")]
public string Error { get; set; }
}


Invoking like this:

var info = JsonConvert.DeserializeObject<Info>(json);


So, is there anyway I can specify, that
code
and
version
under
result
section? I believe I need to use
JsonSerializeSettings
or something like that.

Answer

If you are able to modify your class, then you could create a second class which contains your subproperties:

public class Info
{
    [JsonProperty("result")]
    public Result Result { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }
}

public class Result
{
    [JsonProperty("code")]
    public string Code { get; set; }

    [JsonProperty("version")]
    public string Version { get; set; }
}