2012-02-17 129 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

相關問題