1

I wrote this code,

public JsonResult Index(string query) { return Json(new object[] { "id", "text"}, JsonRequestBehavior.AllowGet); } 

And result,

["id","text"] 

But I want to look like below,

[{"value": 1 , "text": "Amsterdam"}] 

How can I do this? Thanks

1 Answer 1

2

Try creating an anonymous object with the properties...

return Json(new object[] { new { value = 1, text = "Amsterdam" } }, JsonRequestBehavior.AllowGet); 

Or create a class to return a strongly typed array...

private class City { public int value { get; set; } public string text { get; set; } } // ... return Json(new City[] { new City { value = 1, text = "Amsterdam" } }, JsonRequestBehavior.AllowGet); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.