2

Possible Duplicate:
How to set formatting with JavaScriptSerializer when JSON serializing?

I'm using using System.Web.Script.Serialization to serialize some json. However, by default it makes ugly text without indentation.

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var json = serializer.Deserialize<Dictionary<string,dynamic>>(s);

    // do something else to the json...

    //serialize the result
    string serialized = serializer.Serialize(json);
    // ugly text

I'm looking at the second overload and it takes a StringBuilder. I've not used these much. Can I use that to format pretty JSON? How would I do that in this context?

Community
  • 1
  • 1
FlavorScape
  • 13,301
  • 12
  • 75
  • 117

1 Answers1

2

imho, it's not possible to the JavaScriptSerializer how to format the ouput-string (in contradiction to XmlWriter for XmlSerializer)...

in my use-cases i ever decided to go with JSON.net, it's more powerfull, a lot of faster and for JSON ;)

svick
  • 236,525
  • 50
  • 385
  • 514
TheHe
  • 2,933
  • 18
  • 22
  • Does it support dynamic typed objects during deserialization? I hate having to make structs or type definitions to match my json, or lists. – FlavorScape Mar 09 '12 at 23:48
  • I suppose it does: http://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net – FlavorScape Mar 09 '12 at 23:51