2

Using ASP.NET 4.0 C#, how would I be able to change the serialization of a list containing name/value object into a JSON object that is of the format:

{
name:value,
name1:value1,
name2:value2
}

Instead of

{
{name:name, value:value},
{name:name1, value:value1},
{name:name2, value:value2}
}

The name/value object in the dictionary is dynamic in terms of the keys that will exist inside. One workarounds would be to create custom object which will have the explicit properties available - However this approach is not scalable, therefore this question :)

Thanks.

TimLeung
  • 3,459
  • 6
  • 42
  • 59
  • related: http://stackoverflow.com/questions/4256337/how-can-i-return-a-dictionarystring-object-as-jsonresult-and-get-the-proper – Ruben Bartelink Jul 19 '12 at 07:56

1 Answers1

1

You could implement a custom JavaScriptConverter, then you're in full control of the serialization. Or put all your properties into an IDictionary and JSON that and I think it will come out in the format you're after.

batwad
  • 3,588
  • 1
  • 24
  • 38
  • This is exactly what I did - implemented a custom JavascriptConverter that went along with a slight modification of the JsonResult so that I can add my custom JavascriptConverter to the Serialization. – TimLeung Jan 27 '11 at 03:25