我是 WEB API2 和 JSON 的新手。我有一个像这样的 JSON 主体
{
"Input_data": {
"method": "check",
"hashcode": " xxxxxx ",
"accountId": "11111111",
}
}
如何从 POST 请求中检索值?
我有这样的模型
[JsonArray]
public class BaseInput
{
[JsonProperty(PropertyName = "method")]
public string Method { get; set; }
[JsonProperty(PropertyName = "hashcode")]
public string hashCode { get; set; }
[JsonProperty(PropertyName = "accountid")]
public int accountId { get; set; }
}
Controller 代码如下
BaseOutput ApiReqeust(int partnerId,[FromBody] BaseInput Input_data)
并且 Input_data
始终为空。
我做错了什么?
最佳答案
您对该 JSON 输入使用了错误的模型
这更符合您的 JSON 模型
public class InputData {
[JsonProperty("method")]
public string method { get; set; }
[JsonProperty("hashcode")]
public string hashcode { get; set; }
[JsonProperty("accountId")]
public string accountId { get; set; }
}
public class BaseInput {
[JsonProperty("Input_data")]
public InputData Input_data { get; set; }
}
Controller 代码看起来没问题。
关于c# - WEB API2 获取 POST 请求 JSON 数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49944638/