gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:21 24 4
gpt4 key购买 nike

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

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com