0

I'm trying to create a dynamic object from a JSON string in C#. But i can't get it done.

Normally i would get a JSON string through a web service call but in this case I simply created a simple class which I turn into a JSON string. Then I try to turn it back into a dynamic object with the exact same structure as if it was an instance of the Entity class. But that's where I'm having trouble.

This is the class that i turn into a JSON string:

public class Entity
{
    public String Name = "Wut";
    public String[] Scores = {"aaa", "bbb", "ccc"};
}

Then in somewhere in my code i do this:

var ent = new Entity();

// The Serialize returns this:
// "{\"Name\":\"Wut\",\"Scores\":[\"aaa\",\"bbb\",\"ccc\"]}"
var json = new JavaScriptSerializer().Serialize(ent);

dynamic dynamicObject1 = new JavaScriptSerializer().DeserializeObject(json);
dynamic dynamicObject2 = Json.Decode(json);

When I debug this code then i see that the first dynamicObject1 returns a Dictionary. Not really what I'm after.

The second dynamicObject2 looks more like the Entity object. It has a property called Name with a value. It also has a dynamic array called Scores, but for some reason that list turns out to be empty...


Screenshot of empty Scores property in dynamic object:

Dynamic object


So I'm not having any luck so far trying to cast a JSON string to a dynamic object. Anyone any idea how I could get this done?

2
  • Is JavaScriptSerializer a class of your own devising? If so, could you post the code for DeserializeObject and Decode and the dependent functions of these methods? Commented Jan 18, 2014 at 23:11
  • @BolucPapuccuoglu No it's not my code, it's in System.Web.Script.Serialization. And Json.Decode is in System.Web.Helpers. Commented Jan 18, 2014 at 23:13

2 Answers 2

2

Using Json.Net

dynamic dynamicObject1  = JsonConvert.DeserializeObject(json);
Console.WriteLine(dynamicObject1.Name);
Sign up to request clarification or add additional context in comments.

3 Comments

Definitely prefer Json.NET to the built-in .NET JSON serialization framework. I think it's easier to work with and based on published benchmarks, definitely faster.
This is giving me the exact same result as Json.Decode(). The Scores property isn't an array of strings, but has an empty result. Here is a screenshot of the debugged variable: i.imgur.com/11Cknbh.png
@w00 here is the working code dynamic dyn = JsonConvert.DeserializeObject("{\"Name\":\"Wut\",\"Scores\":[\"aaa\",\"bbb\",\"ccc\"]}"); Console.WriteLine(dyn.Scores[0]); and it prints aaa. How can you say same result?
0

Using Json.Net but deserializing into a ExpandableOBject, a type that is native to C#:

 dynamic obj= JsonConvert.DeserializeObject<ExpandoObject>(yourjson);

If the target type is not specified then it will be convert to JObject type instead. Json object has json types attached to every node that can cause problems when converting the object into other dynamic type like mongo bson.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.