如何正确传递和接收json对象?(How to properly pass and receive a json object?)

编程入门 行业动态 更新时间:2024-10-25 04:24:03
如何正确传递和接收json对象?(How to properly pass and receive a json object?)

从客户端,我想将一组json节点: [{key, value},{key, value}]传递给WebAPI端点。 我的api端点param类型应该是什么? 列表<>()或其他什么?

这将是使用C#。

我需要遍历集合中传递的每个端点。

From the client, I would like to pass a collection of json nodes: [{key, value},{key, value}] to a WebAPI endpoint. what should my api endpoint param type be? a List<>() or something else?

This would be using C#.

I need to iterate over each endpoint that is passed in the collection.

最满意答案

通常,最好创建一个将JSON反序列化的模型。 如果您的JSON采用格式,请回答您的问题

[{key1: value1},{key2: value2}]

你可以用一个

List<Dictionary<string,object>>()

如果您确定您的值始终是字符串值,那么您可以这样做

List<Dictionary<string,string>>()

由于JSON值可以是字符串(用引号括起来),整数(没有引号)或null。

所以你的Web API控制器可能是这样的:

[HttpPost] public IHttpActionResult ReceiveJSON([FromBody]List<Dictionary<string,string>> in_json) { // And then one way to iterate over each 'json node' passed foreach(var dict in in_json) { // Do something with dictionary object } return Ok(in_sjon); }

您将使用什么版本的ASP.Net Web API?

In general, it is good practice to create a model that your JSON will be deserialized into. To answer your question though if your JSON was in the format

[{key1: value1},{key2: value2}]

you would be able to use a

List<Dictionary<string,object>>()

If you were sure that your values were always string values you could do

List<Dictionary<string,string>>()

As JSON values can be strings (wrapped in quotes), integers (no quotes) or null.

So your Web API controller could be something like this:

[HttpPost] public IHttpActionResult ReceiveJSON([FromBody]List<Dictionary<string,string>> in_json) { // And then one way to iterate over each 'json node' passed foreach(var dict in in_json) { // Do something with dictionary object } return Ok(in_sjon); }

What version of ASP.Net Web API will you be using?

更多推荐

本文发布于:2023-04-29 10:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336285.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何正确   对象   json   properly   receive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!