javascript C# Web 服务,如何接收 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11534104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Web service, how to receive JSON
提问by Kyle
I made a JSON string with jquery, and i want to send it to a C# web api controller.
我用 jquery 制作了一个 JSON 字符串,我想将它发送到 C# web api 控制器。
This is an example of the JSON object
这是 JSON 对象的示例
{"Name":"","Type":"4","Meals":["2","3"],"Excludes":["Beef","Chicken"]}
I tryed to send it with a URL like this
我试着用这样的 URL 发送它
API/Recipe/Search?json={"Name":"","Type":"4","Meals":["2","3"],"Excludes":["Beef","Chicken"]}
With my Controller like this:
像这样我的控制器:
public class RecipeController : ApiController
{
[HttpGet]
public string Search(searchObject json)
{
return "Asdasd";
}
}
and like this
像这样
public class RecipeController : ApiController
{
[HttpGet]
public string Search(string json)
{
searchObject search = (searchObject)JsonConvert.DeserializeObject(json);
return "Asdasd";
}
}
But in neither case the controller will pick it up. I am using MVC4.
但在这两种情况下,控制器都会接收它。我正在使用 MVC4。
Here is the Jquery i am using to make the call. apiLink is the link I posted above.
这是我用来拨打电话的 Jquery。apiLink 是我在上面发布的链接。
$.getJSON(apiLink, function (data) {
var items = [];
$.each(data, function (key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
How do I get the controller to receive the JSON object?
如何让控制器接收 JSON 对象?
Thanks
谢谢
采纳答案by Habib
You should use POST attribute with the method, this way you will be able to post complex object to the Web API,
您应该在方法中使用 POST 属性,这样您就可以将复杂的对象发布到 Web API,
You may create a class for the JSON, from json to cSharp
您可以为 JSON 创建一个类,从json 到 cSharp
public class SearchObject
{
public string Name { get; set; }
public string Type { get; set; }
public List<string> Meals { get; set; }
public List<string> Excludes { get; set; }
}
Then in your web api, specify the method with HttpPost attribute, Web API will take care of deserialization of json in the post to your template.
然后在您的 web api 中,使用 HttpPost 属性指定方法,web API 将负责将帖子中的 json 反序列化到您的模板。
[HttpPost]
public string Search(SearchObject json)
{
return "Asdasd";
}
You may try fiddler, for making a post request, in the request header specify type:
您可以尝试使用 fiddler 进行发布请求,在请求标头中指定类型:
Content-Type:application/json
内容类型:应用程序/json
and in the request body paste your json, and Execute
并在请求正文中粘贴您的 json,然后执行
回答by Glenn Ferrie
Looks like you already got a response, but here's the code for a working solution:
看起来您已经收到了回复,但以下是有效解决方案的代码:
Note: I used JsonResult Actions in MVC3, but the principles are the same
注:我在MVC3中使用了JsonResult Actions,但原理是一样的
Controller:
控制器:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost] // requires HttpPost attribute
public JsonResult SendData(SearchObject payload)
{
// do something here
return Json(new { status = "Success" });
}
}
public class SearchObject
{
public string Name { get; set; }
public string Type { get; set; }
public List<string> Meals { get; set; }
}
View:
看法:
@{
ViewBag.Title = "Index";
}
<script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajaxSetup({ accepts: "application/json" });
$.ajax({
url: '@Url.Action("SendData")', type: "POST",
success: function (data) {
alert(data.status);
},
error: function (a, b, c) { },
data: { 'Name':'Glenn','Type':'4','Meals':["1","2"] }
});
</script>
<h2>Index</h2>
Hope this helps...
希望这可以帮助...