如何在web api C#中的post方法中处理json数组? [英] How to process the json array in post method in web api C#?

查看:149
本文介绍了如何在web api C#中的post方法中处理json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面提到的HTTPPOST方法中处理json数组数据



I want to process the json array data in the HTTPPOST method mentioned below

[HttpPost]
        [Route("")]
        public async Task<HttpResponseMessage> UploadFile(jsonarraydata)
        {
//how to convert the json array data in objects 
            
    }





我尝试了什么:





What I have tried:

Below is the json array I am passing to post request. I want to convert the array in individual objects so that i can store them in the database.
<pre>[{
  "Name": "sample string 1",
  "Owner": "abc"
}
,	
{
  "Name": "sample string 2",
  "Owner": "xyz"
},
{
  "Name": "sample string 3",
  "Owner": "ags"
},
{
  "Name": "sample string 4",
  "Owner": "sdas"
}
 ]

推荐答案

创建一个具有所需属性的类



Create a Class with the required properties

public class MyType
  {
      public string Name { get; set; }
      public string Owner { get; set; }

  }





并将其作为列表访问。



and access it as a list.

public async Task<HttpResponseMessage> UploadFile(List<MyType> jsonarraydata )
{


这篇关于如何在web api C#中的post方法中处理json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆