Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to return a dynamic object from a json deserialization using json.net? I would like to do something like this:

dynamic jsonResponse = JsonConvert.Deserialize(json);
Console.WriteLine(jsonResponse.message);
share|improve this question
Looks like a duplicate of stackoverflow.com/questions/3142495/… – Jon of All Trades Feb 15 at 15:47

4 Answers

up vote 68 down vote accepted

latest json.net version allow do this:

dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);

output:

 1000
 string
 6

Documentation here: LINQ to JSON with Json.NET

share|improve this answer

I know this is old post but JsonConvert actually has a different method so it would be

var product = new { Name = "", Price = 0 };
var jsonResponse = JsonConvert.DeserializeAnonymousType(json, product);
share|improve this answer
That would be deserializing a json payload into an anonymous type, not a dynamic type. Anonymous types and dynamic types are different things, and I don't believe this addresses the question asked. – jrista Aug 1 '12 at 19:12

As of Json.NET 4.0 Release 1, there is native dynamic support:

[Test]
public void DynamicDeserialization()
{
    var jsonResponse = JsonConvert.DeserializeObject<dynamic>("{\"message\":\"Hi\"}");
    Console.WriteLine(jsonResponse.message); // Hi
}

And, of course, the best way to get the current version is via NuGet.

share|improve this answer

You need to have some sort of type to deserialize to. You could do something along the lines of:

var product = new { Name = "", Price = 0 };
dynamic jsonResponse = JsonConvert.Deserialize(json, product.GetType());

Edit: epitka, there was no need to edit my answer. My answer was based on a solution for .NET 4.0's build in JSON serializer.

Edit2: since epitka is having a cry and down-voted without reason, link to deserialize to anonymous types is here:

http://blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx

share|improve this answer
@epitka - you downvoted me because I un-edited your edit on my answer? blogs.msdn.com/b/alexghi/archive/2008/12/22/… here is the solution. – Phill Jun 7 '11 at 22:54
I don't think I down-voted you. When I downvote somebody, I usually leave a comment why. I don't see why I would down-vote you, although it was in Feb. Anyway, chill out, it is not a world cup. – epitka Jun 8 '11 at 13:30
@epitka - I looked at your reputation, you had -2 for a down-vote on this question yesterday. If you explained why I wouldn't have said anything, it just seemed like a completely unjustified down-vote. – Phill Jun 8 '11 at 14:34
5  
Yes, "I" had it, somebody down voted my answer. I did not down-vote your answer. Maybe we can get answer from SO what happened? I was not even online yesterday at 22:36. I received down-vote for my answer man. – epitka Jun 8 '11 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.