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

Anthony Chu
37.6k10 gold badges85 silver badges72 bronze badges
asked Mar 21, 2014 at 1:56

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);
answered Mar 21, 2014 at 2:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.