Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize without creating wrapper class using json.net

Tags:

c#

json.net

I have started using Json.net recently and changing the existing deserializer from JavaScript to Json.Net

While doing so, I have stuck into one implementation issue.

I have below Json:

string json = @"'Album':{
  'Name': 'Classical',
  'Date': '2005-4-7T00:00:00'
}";

When I am deSerializing it using Json.net, I am getting a null response:

var a = JsonConvert.DeserializeObject<Album>(json);

I look for help and found that to deserialize it, I need to create a wrapper class to which Album must be a property.

However, I have many such classes to deserialize it. Is there any generic way to do it? Do I need to create wrapper for all my classes like this:

public class JsonOutputWrapper
{            
    public Album Album{ get; set; }
}

Can I make this work with some generic implementation without creating wrapper class:

var a = JsonConvert.DeserializeObject<Album>(json);
like image 677
PANKAJ Avatar asked Sep 19 '15 19:09

PANKAJ


2 Answers

As others have mentioned, your JSON is not valid.

Suppose the valid JSON is: { "Album": { "Name": "Classical", "Date": "2005-4-7T00:00:00" } }

Then you can do this:

var json = "{ \"Album\": { \"Name\": \"Classical\", \"Date\": \"2005-4-7T00:00:00\" } }";
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var album = jtoken.SelectToken("Album").ToObject<Album>();

or you can also use dynamic:

var json = "{ \"Album\": { \"Name\": \"Classical\", \"Date\": \"2005-4-7T00:00:00\" } }";
var album = JsonConvert.DeserializeObject<dynamic>(json).Album.ToObject<Album>();
like image 181
Søren Kruse Avatar answered Oct 13 '22 11:10

Søren Kruse


Well, Certainly your json is not Completely valid and needs a pair of {} surrounding it.

I would say the suggestion in this post is more like a hack than a proper solution, but if you are sure that the only missing part of your json is the outer object part, then we can create a helper class and use JObject for parsing the json and then convert the inner object to specified type.

class JsonHelper
{
    private const string JsonFormat = "{{{0}}}";

    public static T Deserialize<T>(string json,string name)
    {
        var jObj = JObject.Parse(string.Format(JsonFormat, json));
        var obj = jObj[name].ToObject<T>();

        return obj;
    }

    public static T Deserialize<T>(string json)
    {
        return Deserialize<T>(typeof (T).Name, json);
    }
}

here is an usage example:

class Program
{
    static void Main(string[] args)
    {
        string json = @"'Album':{
                             'Name': 'Classical',
                             'Date': '2005-4-7T00:00:00'
                            }";

        var album = JsonHelper.Deserialize<Album>(json);
        //or with name
        var album2 = JsonHelper.Deserialize<Album>(json,"Album");
   }
}
like image 2
user3473830 Avatar answered Oct 13 '22 09:10

user3473830