首页 > 解决方案 > 发布到另一个 API 时更改值

问题描述

我正在编写一个将发布到外部 API 的 API 端点,但我不确定要用于我的 viewModel 的数据类型

我尝试了各种操作 ViewModel 和 JsonObject 的方法,但我目前没有任何进展可以显示更接近预期结果这是在 .NET Core 2、web api 项目上运行的。

我创建的用于保存数据的类:

public class ViewModel
    {
        public int AccountIds { get; set; }
        public string Title { get; set; }
        public string Category { get; set; }
    }

外部 API 期望的 JSON:

{
    "AccountIDs": [4],
    "Title":"value",
    "Category": "value"
}

当我尝试将其发布到我的 API 时,我收到以下错误:

    "": [
        "JsonToken EndArray is not valid for closing JsonType Object. Path '', line 2, position 18."
    ],
    "AccountIDs": [
        "Unexpected character encountered while parsing value: [. Path 'AccountIDs', line 2, position 16."
    ]
}```

标签: c#json.net-coredotnet-httpclient

解决方案


外部 API的AccountIDs属性需要一个整数数组 ( "AccountIDs": [4])。所以声明你的模型:

public class ViewModel
    {
        public int[] AccountIDs { get; set; }
        public string Title { get; set; }
        public string Category { get; set; }
    }

推荐阅读