c# - 反序列化JSON时遇到Null

标签 c# json

我有 json 消息

{"code":200,
"description":{
 "15":{"id":"15","name":"US"},
 "25":{"id":"25","name":"Canada"},
 "msg":"Ok"}}

我正在尝试用这样的类反序列化它

public class NewCountry
{
    public string id { get; set; }
    public string name { get; set; }
}

public class NewCountryDescription
{
    public List<NewCountry> Countries{ get; set; }
    public string msg { get; set; }
}

public class RootObject
{
    public int code { get; set; }
    public NewCountryDescription description { get; set; }
}

var ListOfCountries = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(res);

但是我在 NewCountry 总是得到 null 我做错了什么?

最佳答案

如果您从 json 中删除 msg 或将其移至其他位置(不在描述中),它将起作用:

    {"code":200,
"description":{
 "15":{"id":"15","name":"US"},
 "25":{"id":"25","name":"Canada"}
 }}


    public class RootObject
    {
        public int code { get; set; }
        public Dictionary<string, NewCountry> description { get; set; }
    }

    public class NewCountry
    {
        public string id { get; set; }
        public string name { get; set; }
    }

关于c# - 反序列化JSON时遇到Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34001022/

相关文章:

c# tooltip backcolor 和 forecolor 不随 using 方法或其他方式改变

c# - MVC4 捆绑缓存 header

java - 如何循环 json 并将每个索引发布到camelcontext中

c# - Controller 映射中的 ASP.NET MVC 枚举参数

c# - 在 chrome 中滚动 ListBox 时回发

javascript - 等待多个请求完成

javascript - jQuery mobile - 从 json 文件加载到 ListView 的数据刷新后会中断

c# - 使用 "random"键反序列化 JSON

json - grails 3 json序列化器没有类属性

c# - 如何更改 DataTable 的列名?