简体   繁体   English

C#Http Post从正文接收json

[英]C# Http Post receive json from body

I have this HttpPost method: 我有这个HttpPost方法:

[HttpPost]    
public string Test([FromBody]List<Account> accounts)
{
   var json = JsonConvert.SerializeObject(accounts);
   Console.Write("success");
   return json;
}

and this is my Account class: 这是我的Account类:

public class Account
{
    public int accountId;
    public string accountName;
    public string createdOn;
    public string registrationNumber;
}

This is my json file which i send with postman: 这是我与邮递员发送的json文件:

{
    "Account": [
    {
        "accountId": "1",
        "accountName": "A purple door",
        "createdOn": "25-07-2017",
        "registrationNumber": "purple"
    },
    {
        "accountId": "2",
        "accountName": "A red door",
        "createdOn": "26-07-2017",
        "registrationNumber": "red"
    },
    {
        "accountId": "3",
        "accountName": "A green door",
        "createdOn": "27-07-2017",
        "registrationNumber": "green"
    },
    {
        "accountId": "4",
        "accountName": "A yellow door",
        "createdOn": "25-07-2017",
        "registrationNumber": "yellow"
    }
    ]
}

If i send this json my method doesn't work and it returns a null object. 如果我发送此json,则我的方法无效,它将返回一个空对象。 The only way to make it work is by sending the object only without the "Account" like this: 使其正常工作的唯一方法是仅发送不带“帐户”的对象,如下所示:

[
    {
      "accountId": "1",
      "accountName": "A purple door",
      "createdOn": "25-07-2017",
      "registrationNumber": "purple"
    },
    {
      "accountId": "2",
      "accountName": "A red door",
      "createdOn": "26-07-2017",
      "registrationNumber": "red"
    },
    {
      "accountId": "3",
      "accountName": "A green door",
      "createdOn": "27-07-2017",
      "registrationNumber": "green"
    },
    {
      "accountId": "4",
      "accountName": "A yellow door",
      "createdOn": "25-07-2017",
      "registrationNumber": "yellow"
    }
]

But i want the previous file format. 但是我想要以前的文件格式。 How could my method receive the previous JSON ? 我的方法如何接收以前的JSON?

Try to use this contract to achieve your requirement. 尝试使用此合同来满足您的要求。

public class Rootobject
{
    public Account[] Account { get; set; }
}

public class Account
{
    public string accountId { get; set; }
    public string accountName { get; set; }
    public string createdOn { get; set; }
    public string registrationNumber { get; set; }
}

Method should be like this. 方法应该是这样的。

[HttpPost]    
public string Test([FromBody]Rootobject accounts)
{
   var json = JsonConvert.SerializeObject(accounts);
   Console.Write("success");
   return json;
}

Add a wrapper for you class Account and change the method defination 为您的Account类添加一个包装器并更改方法定义

public class Account
        {
            public int accountId;
            public string accountName;
            public string createdOn;
            public string registrationNumber;
        }
        public class AccountWrapper
        {
            public List<Account> Accounts { get; set; }
        }
public string Test([FromBody]AccountWrapper accounts)
    {

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM