c# - 发布到另一个 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."
]
}```
解决方案
外部 API的AccountIDs
属性需要一个整数数组 ( "AccountIDs": [4]
)。所以声明你的模型:
public class ViewModel
{
public int[] AccountIDs { get; set; }
public string Title { get; set; }
public string Category { get; set; }
}
推荐阅读
- sql - 如何正确写出复杂的逻辑条件?
- linux - 如何根据错误关键字从日志文件中提取行?
- python - 如何并排遍历列表中的字符串元素?
- python - Sendgrid 使用 API 密钥进行身份验证
- javascript - office-js中的Excel函数解析
- artifactory - Artifactory 从 6.14.1 升级到 7.10.2:migrate.sh 找不到旧的 Artifactory 版本
- argo-workflows - 如何为 Argo Workflow DAG 中的每个步骤传递不同的参数值
- flexbox - Flexbox、uk-flex-left 和 uk-flex-right
- docusignapi - 签名后 HTML 文档缩小
- hyperledger-fabric - 隐式策略评估失败 - ... 需要满足 1 个“作家”子策略:权限被拒绝