asp.net中的json序列化mvc c#

pil*_*ili 3 c# asp.net-mvc json c#-4.0 asp.net-mvc-3

public ActionResult About()
{
List listStores = new List();
listStores = this.GetResults(“param”);
return Json(listStores, “Stores”, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码我可以得到以下结果:

[{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555",
  "email":"abc@ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"}},
 {"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454",
"email":"nfnf@ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"}},
Run Code Online (Sandbox Code Playgroud)

我怎么能以下面的格式得到结果?在结果的开头需要商店.

{
"stores" : [
{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555",
     "email":"abc@ac.com",
     "geo":{"latitude":"12.9876","longitude":"122.376237"}},
{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454",
     "email":"nfnf@ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"
}} ] }
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 8

尝试

return Json(new { stores = listStores }, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)

在上面的语句中,您将使用名为"stores"的属性创建一个新对象,然后使用列表中的项目数组填充该对象.

你也可以使用一个类,定义如下:

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

    public StoreListJsonDTO(List storeList)
    {
        this.Stores = storeList;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中,你会做:

var result = new StoreListJsonDTO(listStores);
return Json(result, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)