views:

52

answers:

2

Let's say I have a Dictionary<string,object> like so:

var foo = new Dictionary<string, object>();
foo["bar"] = new
{
    Quux = "bacon",
    Quinge = 42
};
foo["baz"] = DateTime.Now;

I expect it the result to the user would be akin to:

{"bar":{"Quux":"bacon","Quinge":42},"baz":"2009-12-02 17:23:00"}

However, it could just as easily be:

[{"Key":"bar","Value":{"Quux":"bacon","Quinge":42}},
{"Key":"baz","Value":"2009-12-02 17:23:00"}]

Which will it be, and if it's the latter, what do I need to do to ensure that I get the former?

+1  A: 

It's like the first one, although the property names don't have quotes. Also dates dont get serialized like that, instead you get a call to the Date() javascript function.

Pablote
+2  A: 

It returns:

{"bar":{"Quux":"bacon","Quinge":42},"baz":"\/Date(1259795535958)\/"}

If you need to do any JSON stuff in .NET I would recommend having a gander at the Json.NET library.

HTHs,
Charles

Charlino
Quite happy with the JSON features I get as part of ASP.NET MVC, tyvm.
Chris Charabaruk
Sure. You are aware that I got that result from the built in ASP.NET MVC stuff? Was simple to spike. I was merely suggesting a library that can give you advanced JSON serializing and parsing, if you so happened to need it.
Charlino
With Json.net you also get linq capabilities. Very handy.
jfar