Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 从MVC控制器返回Json数据_Asp.net Mvc_Json - Fatal编程技术网

Asp.net mvc 从MVC控制器返回Json数据

Asp.net mvc 从MVC控制器返回Json数据,asp.net-mvc,json,Asp.net Mvc,Json,我怎样才能得到以下格式的结果 [{"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":"

我怎样才能得到以下格式的结果

[{"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"}},
我想让
在数据的开头存储


请在这方面帮助我。

您需要创建一个对象,该对象包含名为stores的属性中的存储:

{

"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"}} ] 
}

我在这里使用了一个匿名类型,如果在多个地方需要这个结果类型,您可以考虑为它创建一个“适当”类。

代码>公共类存储视图模型{
public ActionResult About()
{
    var result = new { stores = this.GetResults("param") };
    return Json(result, "Stores", JsonRequestBehavior.AllowGet);
}
公共列表存储{get;set;} } 关于()的公共行动结果 { List listStores=新列表(); listStores=this.GetResults(“参数”); StoresViewModel=newstoresviewmodel(){ 商店=列表商店; } 返回Json(model,JsonRequestBehavior.AllowGet); }
JavaScriptSerializer
可从命名空间
System.Web.Script.Serialization

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);
}

如果您想将对象以Json格式发送到客户端 像数据表、列表、字典等,然后需要重写jsonResult和ExecuteSult

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

return Json(new { stores: jsonStores }, "Stores", JsonRequestBehavior.AllowGet);
其他明智的做法是使用linq格式返回数据 像

使用JSON.NET(必须使用override jsonResult和ExecuteSult)


使用linq的其他选项

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

尽管您的答案可能会解决问题,但请尝试解释
为什么它解决了问题。指出一些你为使它工作而改变/修复的东西。这将有助于OP
理解
为什么你的答案能解决他的问题。
  DataTable dt = new DataTable();//some data in table
   return json("data",JsonConvert.SerializeObject(dt))
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;
                }
            }
        }