Json serialization in asp.net mvc c #

public ActionResult About()
List listStores = new List();
listStores = this.GetResults("param");
return Json(listStores, "Stores", JsonRequestBehavior.AllowGet);


Using the above code, I can get the following output:



how can i get the result in below format? We need shops at the beginning of the result.

"stores" : [
}} ] }



source to share

1 answer


return Json(new { stores = listStores }, JsonRequestBehavior.AllowGet);


In the above statement, you create a new object with a property named Stores, which is then populated with an array of items from the list.

You can also use a class, something defined like this:

public class StoreListJsonDTO
    [DataMember(Name = "stores")]
    public List Stores { get; set; }

    public StoreListJsonDTO(List storeList)
        this.Stores = storeList;


Then, in your code, you must:

var result = new StoreListJsonDTO(listStores);
return Json(result, JsonRequestBehavior.AllowGet);




All Articles