Bogdan Verbenets
Bogdan Verbenets

Reputation: 26976

JsonResult with numbers as field names

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?

Upvotes: 2

Views: 258

Answers (1)

Adrian Iftode
Adrian Iftode

Reputation: 15673

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"}

Upvotes: 4

Related Questions