簡體   English   中英

如何將JSON發送到使用模型作為HTTPPOST中的inputParam的WEBAPI中

[英]How to send JSON into a WEBAPI that uses a model as inputParam from the HTTPPOST

我已經在我的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."
        ]
},

我該如何解決該錯誤?

您的原始JSON格式錯誤,應采用以下格式:

{
    "apiKey": "xxx",
    "Brand": {
        "BrandName": "xx"
    },
    "query": "showBrand"
}

額外的幫助,對於您的使用者對象,您的json格式如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM