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"}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top