从客户端,我想将一组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?
更多推荐
发布评论