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



在过去的几天里,我一直在玩这个游戏,我希望有人能说明问题所在。

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

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);

我得到的错误信息是:

无法将当前JSON对象(例如{"name":"value"})反序列化为类型"System.Collections.Generic.List`1[WebApplication1.EventItems]",因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。

要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或者更改反序列化的类型,使其成为可以从JSON对象反序列化的普通.NET类型(例如,不是像integer这样的基元类型,也不是像array或List这样的集合类型)。JsonObjectAttribute也可以添加到类型中,以强制它从JSON对象反序列化。

路径"events",第1行,位置10。

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

请参阅如何使用"反序列化JSON数组;根";使用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);

在第一种情况下,json是对象数组,所以反序列化为类类型列表成功。在第二种情况下,json是一个对象,其"events"属性设置为对象数组,因此无法将其反序列化为列表。

你可以做的是更改你的类声明:

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; }        
}
public class EventItems
{
    public EventItem[] Events { get; set; }
}

并将其反序列化:

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

只需从第二个JSON字符串中删除对象事件

string eventItem = "[{"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"}]";

在这两个示例中,您的JSON字符串似乎不一样。在第一个例子中,您使用了一个简单的JSON数组:

[
    {
        "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"
    }
]

在第二个例子中,您将数组分配给一个对象(events):

{
    "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"
        }
    ]
}

相关内容

  • 没有找到相关文章

最新更新