6

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);
3
  • 1
    Your JSON string is invalid. Commented Sep 19, 2015 at 19:20
  • Yes, please consider it as string json = @"'Album':{ 'Name': 'Classical', 'Date': '2005-4-7T00:00:00' }"; Commented Sep 19, 2015 at 19:42
  • try this: { "Album": { "Name": "Classical", "Date": "2005-4-7T00: 00: 00" } } Commented Sep 19, 2015 at 19:57

2 Answers 2

3

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>();
Sign up to request clarification or add additional context in comments.

Comments

2

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");
   }
}

1 Comment

Last line should be var album2 = JsonHelper.Deserialize<Album>(json, "Album");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.