JsonResult with numbers as field names
-
30-05-2021 - |
题
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?
解决方案
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"}
不隶属于 StackOverflow