JsonResult with numbers as field names
-
30-05-2021 - |
Question
I need to return a JSON result from my ASP.NET MVC 3 application like this:
{ 0: value1, 1: value2 }
But I can't create an object with anonymous type that would have numbers as field names. So what can I do?
Solution
var dict = new Dictionary<string,string>();
dict["0"] = "value1";
dict["1"] = "value2";
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var output = serializer.Serialize(dict);
The output is
{"0":"value1","1":"value2"}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow