我已经在HTTP POST方法中为输入参数定义了一个模型
public ActionResult<JObject> Post([FromBody] APIInputObject apiInputObject)
模型看起来像这样
public class APIInputObject
{
public string ApiKey { get; set; }
public Brand Brand { get; set; }
public string Query { get; set; }
public string Locale { get; set; } = "SE";
public string UseFormatter { get; set; }
}
品牌部分进一步定义为这样
public class Consumer
{
public string ConsumerName { get; set; }
public List<Brand> Brands { get; set; }
}
public class Brand
{
public string BrandName { get; set; }
}
现在的问题是,当我发送JSON时,看起来像下面的错误
{
"apiKey": "xxx",
"Brand": "xx",
"query": "showBrand"
}
错误如下
{
"errors": {
"Brand": [
"Error converting value \"xx\" to type 'Pim.Models.Brand'. Path 'Brand', line 3, position 17."
]
},
该如何解决该错误?
答案 0 :(得分:1)
您原来的JSON格式错误,应采用以下格式:
{
"apiKey": "xxx",
"Brand": {
"BrandName": "xx"
},
"query": "showBrand"
}
奖金帮助,对于您的消费者对象,您的json格式如下:
{
"ConsumerName": "xxx",
"Brands": [{
"BrandName": "xx1"
},
{
"BrandName": "xx2"
}]
}