2

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"

dbc
  • 104,963
  • 20
  • 228
  • 340
user1789782
  • 187
  • 1
  • 9
  • 1
    You mean you want to deserialize the value of `"result"` without needing an outer wrapper class? If so, see [Deserializing JSON - how to ignore the root element?](https://stackoverflow.com/questions/13702657) or [JSON.NET deserialize a specific property](https://stackoverflow.com/questions/19438472). – dbc Apr 19 '16 at 23:09
  • 1
    Possible duplicate of [Deserializing JSON - how to ignore the root element?](http://stackoverflow.com/questions/13702657/deserializing-json-how-to-ignore-the-root-element) – dbc Apr 20 '16 at 05:30

2 Answers2

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

Console.WriteLine(_Data.status);

Fiddle: https://dotnetfiddle.net/gjYS2p

Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
0

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; }
}
user1789782
  • 187
  • 1
  • 9