2012-02-17 166 views
3
public ActionResult About() 
    { 
     List<Stores> listStores = new List<Stores>(); 
     listStores = this.GetResults("param"); 
     return Json(listStores, "Stores", JsonRequestBehavior.AllowGet); 
    } 

使用上面的代码的JSON数据,我能得到以下结果返回从MVC控制器

[{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555","email":"[email protected]","geo":{"latitude":"12.9876","longitude":"122.376237"}}, 

{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454","email":"[email protected]","geo":{"latitude":"12.9876","longitude":"122.376237"}}, 

如何将我能得到以下结果格式?

{ 

"stores" : [ 
{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555","email":"[email protected]", 
"geo":{"latitude":"12.9876","longitude":"122.376237"}}, 

{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454","email":"[email protected]","geo":{"latitude":"12.9876","longitude":"122.376237"}} ] 
} 

我想要在数据的开头有stores

请在这方面帮助我。

回答

5

您需要创建一个包含属性命名卖场内的存储对象:

public ActionResult About() 
{ 
    var result = new { stores = this.GetResults("param") }; 
    return Json(result, "Stores", JsonRequestBehavior.AllowGet); 
} 

我在这里使用匿名类型为简单起见,如果这个结果被类型在多个地方需要你可能考虑为它创建一个“适当”的类。

+0

像我这样做

DataTable dt = new DataTable();//some data in table return json("data",JsonConvert.SerializeObject(dt)) 

其他选项.. 。要学会更快地输入:) – Tr1stan 2012-02-17 10:48:26

0
public class StoresViewModel{ 
    public List<Stores> stores {get;set;} 
} 


public ActionResult About() 
{ 
     List<Stores> listStores = new List<Stores>(); 
     listStores = this.GetResults("param"); 
     StoresViewModelmodel = new StoresViewModel(){ 
      stores = listStores; 
     } 
     return Json(model, JsonRequestBehavior.AllowGet); 
} 
1

JavaScriptSerializer可以从命名空间System.Web.Script.Serialization

var ser = new JavaScriptSerializer(); 
var jsonStores = ser.Serialize(stores); 

return Json(new { stores: jsonStores }, "Stores", JsonRequestBehavior.AllowGet); 
1

发现,如果要发送对象的客户端为JSON格式 像数据表格,列表,字典等,则需要重写jsonResult和的ExecuteReuslt其他

明智使用LINQ格式返回数据 像

使用JSON.NET(必须要使用替代jsonResult和的ExecuteReuslt)使用LINQ

var Qry = (from d in dt.AsEnumerable() 
           select new 
           { 
            value = d.Field<int>("appSearchID"), 
            text = d.Field<string>("appSaveSearchName"), 
            type = d.Field<int>("appSearchTypeStatus") 
           }); 
        return json("Data", Qry); 

重写方法

protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) 
     { 
      try 
      { 
        return new JsonNetResult 
        { 
         Data = data, 
         ContentType = contentType, 
         ContentEncoding = contentEncoding, 
         JsonRequestBehavior = behavior, 
         MaxJsonLength = int.MaxValue 
        }; 

      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 

    public class JsonNetResult : JsonResult 
     { 
      public override void ExecuteResult(ControllerContext context) 
      { 
       try 
       { 
       HttpResponseBase response = context.HttpContext.Response; 
        response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType; 
        if (this.ContentEncoding != null) 
         response.ContentEncoding = this.ContentEncoding; 
        if (this.Data == null) 
         return; 
        using (StringWriter sw = new StringWriter()) 
        { 
         response.Write(this.Data); 
        } 
       } 
       catch (Exception) 
       { 
        throw; 
       } 
      } 
     } 
+0

虽然你的答案可能会解决这个问题,但试着解释一下为什么它解决了这个问题。指出一些你已经改变/修复的事情,以使其发挥作用。这将帮助OP“理解”为什么你的答案解决了他的问题。 – Mathlight 2015-04-18 06:12:12