Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将JSON发送到使用模型作为HTTPPOST的inputParam的WEBAPI中_C#_Json.net - Fatal编程技术网

C# 如何将JSON发送到使用模型作为HTTPPOST的inputParam的WEBAPI中

C# 如何将JSON发送到使用模型作为HTTPPOST的inputParam的WEBAPI中,c#,json.net,C#,Json.net,我在HTTPPOST方法中为输入参数定义了一个模型,如下所示 public ActionResult<JObject> Post([FromBody] APIInputObject apiInputObject) public class APIInputObject { public string ApiKey { get; set; } public Brand Brand { get; set; } public string Query { get;

我在HTTPPOST方法中为输入参数定义了一个模型,如下所示

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; }
}
品牌部分进一步定义如下

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; }
}
错误如下

{
    "errors": {
        "Brand": [
            "Error converting value \"xx\" to type 'Pim.Models.Brand'. Path 'Brand', line 3, position 17."
        ]
},

如何修复此错误?

您的原始JSON格式错误,应采用以下格式:

{
    "apiKey": "xxx",
    "Brand": {
        "BrandName": "xx"
    },
    "query": "showBrand"
}
额外帮助,对于您的消费者对象,您的json格式如下:

{
    "ConsumerName": "xxx",
    "Brands": [{
        "BrandName": "xx1"
    },
    {
        "BrandName": "xx2"
    }]
}

请注意,午夜停止编码:)