【问题标题】:Unable to deserialise result to model无法将结果反序列化为模型
【发布时间】:2022-12-09 01:30:07
【问题描述】:

我有以下课程:

public class Countries
{
    public List<Country> countries { get; set; }
}

public class Country
    {
    public string countryName { get; set; }
    public string region { get; set; }
    public string code { get; set; }
}

这是通过 HttpRequest 读入的:

但是当我尝试反序列化结果时:

var Mycountries = JsonConvert.DeserializeObject<Countries>(body);

但是 Mycountries 变量始终为空。

显然我遗漏了一些明显的东西,我想知道是否有人可以帮助我吗?

【问题讨论】:

  • 你的课程是国家和国家,但你的 json 有数据。

标签: c# asp.net-core model-view-controller


【解决方案1】:

看起来你的 json 是 Country 对象的数组或列表,而不是 Countries 对象,这意味着你必须将它分配给 Mycountries.countries:

var Mycountries = new Countries();
Mycountries.countries = JsonConvert.DeserializeObject<List<Country>>(body);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多