简体   繁体   中英

Newtonsoft.Json Custom Root Name for Deserialization

I have this class that defines the json format:

public class ResultType
{
    public bool status { get; set; }
    public string message { get; set; }
}

The actual json looks like this:

{"result":{"status":true,"message":"Success"}}

How can I override the root attribute when de-serializing the json to "result"

JObject jsonResponse = JObject.Parse(jsonString);
ResultType _Data = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultType>(jsonResponse["result"].ToString());

Console.WriteLine(_Data.status);

Fiddle: https://dotnetfiddle.net/gjYS2p

I have a central deserialization method, so I'm trying to avoid type specific code as much as possible.

I used the following to resolve the problem, maybe not as sexy as I was hoping for but it works.

public class ResultType
{
    public ResultDetailType result { get; set; }
}
public class ResultDetailType
{ 
    public bool status { get; set; }
    public string message { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM