c# - 将 JSON 数据保存到 C# 自定义对象中

标签 c# json properties json.net deserialization

过去几天我一直在研究这个问题,我希望有人能阐明问题所在。

我创建了这个自定义对象:

public class WorldInformation
{
    public string ID { get; set; }
    public string name { get; set; }
}

和这个 JSON 数据:

string world = "[{\"id\":\"1016\",\"name\":\"Sea of Sorrows\"}, {\"id\":\"1008\",\"name\":\"Jade Quarry\"},{\"id\":\"1017\",\"name\":\"Tarnished Coast\"},{\"id\":\"1006\",\"name\":\"Sorrow's Furnace\"},{\"id\":\"2014\",\"name\":\"Gunnar's Hold\"}]";

我可以通过反序列化将数据成功保存在我的自定义对象中:

List<WorldInformation> worlds = JsonConvert.DeserializeObject<List<WorldInformation>>(world);

但是……

当我像这样创建自定义对象时

public class EventItems
{
    public string World_ID { get; set; }
    public string Map_ID { get; set; }
    public string Event_ID { get; set; }
    public string State { get; set; }        
}

并且有像这样的 JSON 数据:

string eventItem = "{\"events\":[{\"world_id\":1011,\"map_id\":50,\"event_id\":\"BAD81BA0-60CF-4F3B-A341-57C426085D48\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":50,\"event_id\":\"330BE72A-5254-4036-ACB6-7AEED05A521C\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"0AC71429-406B-4B16-9F2F-9342097A50AD\",\"state\":\"Preparation\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"C20D9004-DF6A-4217-BF25-7D6B5788A94C\",\"state\":\"Success\"}]}";

反序列化时出错

 List<EventItems> events = JsonConvert.DeserializeObject<List<EventItems>>(eventItem);

我得到的错误信息是:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WebApplication1.EventItems]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'events', line 1, position 10.

最佳答案

不幸的是,没有办法像 XmlSerializer 那样指定根 Json 元素。

参见 How to deserialize JSON array with "root" element for each object in array using Json.NET?

public class EventItems
{
    public EventItems()
    {
        Events = new List<EventItem>();
    }

    public List<EventItem> Events { get; set; }
}

public class EventItem
{
    public string World_ID { get; set; }
    public string Map_ID { get; set; }
    public string Event_ID { get; set; }
    public string State { get; set; }        
}

用法:

string eventItem = "{\"events\":[{\"world_id\":1011,\"map_id\":50,\"event_id\":\"BAD81BA0-60CF-4F3B-A341-57C426085D48\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":50,\"event_id\":\"330BE72A-5254-4036-ACB6-7AEED05A521C\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"0AC71429-406B-4B16-9F2F-9342097A50AD\",\"state\":\"Preparation\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"C20D9004-DF6A-4217-BF25-7D6B5788A94C\",\"state\":\"Success\"}]}";

var items = JsonConvert.DeserializeObject<EventItems>(eventItem);

关于c# - 将 JSON 数据保存到 C# 自定义对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17505203/

相关文章:

c# - 需要帮助在 C# 中为链表制作枚举器

json - 如何在 dart 中映射动态 json api 响应

java - 如何将特定数据从 JSON 文件解析为 html 下拉菜单

javascript - ecmascript 5 或 6 中未定义键和空键的行为

python - 为什么 property.fget 是只读属性?

c# - 一次检查 3 个枚举值?

c# - 什么是 Control 的 DesignMode 属性以及如何使用它?

C# 最佳实践 : Is it ok to have more than one Settings file?

javascript - Array.push() 如果不存在?

swift - Swift 3 中的 Depreciation++ 运算符,寻找解决方法