【问题标题】:Changing value when posting to anotherAPI发布到另一个 API 时更改值
【发布时间】:2019-07-23 12:54:14
【问题描述】:

我正在编写一个将发布到外部 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."
    ]
}```

【问题讨论】:

  • 在 JSON 中 AccountIds 是一个数组。更新视图模型public int[] AccountIds { get; set; }

标签: c# json .net-core dotnet-httpclient


【解决方案1】:

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

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

【讨论】:

  • 我在阅读您的帖子前不久就想到了这一点,但您是对的。感谢您的回答。
猜你喜欢
  • 2016-02-26
  • 2017-04-01
  • 1970-01-01
  • 2021-03-19
  • 2010-10-17
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多