In PHP to return some JSON I would do:
在PHP中返回一些JSON,我会这样做:
return json_encode(array('param1'=>'data1','param2'=>'data2'));
return json_encode(array('param1'=>'data1','param2'=>'data2'));
how do I do the equivalent in C# ASP.NET MVC3 in the simplest way?
我如何以最简单的方式在C#ASP.NET MVC3中做等效的操作?
4 个解决方案
#1
8
You could use the JavaScriptSerializer class which is built-in the framework. For example:
您可以使用框架内置的JavaScriptSerializer类。例如:
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new { param1 = "data1", param2 = "data2" });
yields:
收益率:
{"param1":"data1","param2":"data2"}
But since you talked about returning JSON in ASP.NET MVC 3 there are already built-in techniques that allow you to directly return objects and have the underlying infrastructure take care of serializing this object into JSON to avoid polluting your code with such plumbing.
但是,由于您谈到在ASP.NET MVC 3中返回JSON,因此已经有内置技术允许您直接返回对象并让底层基础结构负责将此对象序列化为JSON,以避免使用此类管道污染您的代码。
For example in ASP.NET MVC 3 you simply write a controller action returning a JsonResult
:
例如,在ASP.NET MVC 3中,您只需编写一个返回JsonResult的控制器操作:
public ActionResult Foo()
{
// it's an anonymous object but you could have used just any
// view model as you like
var model = new { param1 = "data1", param2 = "data2" };
return Json(model, JsonRequestBehavior.AllowGet);
}
You no longer need to worry about plumbing. In ASP.NET MVC you have controller actions that return action results and you pass view models to those action results. In the case of JsonResult it's the underlying infrastructure that will take care of serializing the view model you passed to a JSON string and in addition to that properly set the Content-Type
response header to application/json
.
你不再需要担心管道问题了。在ASP.NET MVC中,您具有返回操作结果的控制器操作,并将视图模型传递给这些操作结果。在JsonResult的情况下,它是底层基础结构,它将负责序列化您传递给JSON字符串的视图模型,并且除了正确设置Content-Type响应头到application / json之外。
#2
4
I always use JSON .Net: http://json.codeplex.com/ and the documentation: http://james.newtonking.com/projects/json/help/
我总是使用JSON .Net:http://json.codeplex.com/和文档:http://james.newtonking.com/projects/json/help/
#4
2
The simplest way it may be like this:
最简单的方式可能是这样的:
public JsonResult GetData()
{
var myList = new List<MyType>();
//populate the list
return Json(myList);
}
#1
8
You could use the JavaScriptSerializer class which is built-in the framework. For example:
您可以使用框架内置的JavaScriptSerializer类。例如:
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new { param1 = "data1", param2 = "data2" });
yields:
收益率:
{"param1":"data1","param2":"data2"}
But since you talked about returning JSON in ASP.NET MVC 3 there are already built-in techniques that allow you to directly return objects and have the underlying infrastructure take care of serializing this object into JSON to avoid polluting your code with such plumbing.
但是,由于您谈到在ASP.NET MVC 3中返回JSON,因此已经有内置技术允许您直接返回对象并让底层基础结构负责将此对象序列化为JSON,以避免使用此类管道污染您的代码。
For example in ASP.NET MVC 3 you simply write a controller action returning a JsonResult
:
例如,在ASP.NET MVC 3中,您只需编写一个返回JsonResult的控制器操作:
public ActionResult Foo()
{
// it's an anonymous object but you could have used just any
// view model as you like
var model = new { param1 = "data1", param2 = "data2" };
return Json(model, JsonRequestBehavior.AllowGet);
}
You no longer need to worry about plumbing. In ASP.NET MVC you have controller actions that return action results and you pass view models to those action results. In the case of JsonResult it's the underlying infrastructure that will take care of serializing the view model you passed to a JSON string and in addition to that properly set the Content-Type
response header to application/json
.
你不再需要担心管道问题了。在ASP.NET MVC中,您具有返回操作结果的控制器操作,并将视图模型传递给这些操作结果。在JsonResult的情况下,它是底层基础结构,它将负责序列化您传递给JSON字符串的视图模型,并且除了正确设置Content-Type响应头到application / json之外。
#2
4
I always use JSON .Net: http://json.codeplex.com/ and the documentation: http://james.newtonking.com/projects/json/help/
我总是使用JSON .Net:http://json.codeplex.com/和文档:http://james.newtonking.com/projects/json/help/
#3
#4
2
The simplest way it may be like this:
最简单的方式可能是这样的:
public JsonResult GetData()
{
var myList = new List<MyType>();
//populate the list
return Json(myList);
}