Deserializing JSON error: the type requires a JSON object

Deserializing JSON error: the type requires a JSON object

我正在尝试反序列化 JSON 响应,但我在创建数据模型时做错了。

我用EventsData resultJSON = JsonConvert.DeserializeObject<EventsData>(jsonText);

正常反序列化

JSON 包含事件列表,例如:

{
   "@event_no":"6924",
   "@name":"REDACTED",
    "Show":{
        "@show_no":"1",
        "@show_datetime":"2007-04-05 15:00:00"
     }
 },
 {
    "@event_no":"6925",
    "@name":"REDACTED",
     "Show":{
         "@show_no":"1",
         "@show_datetime":"2007-04-15 15:00:00"
      }
 }

我的数据模型:

public class Show
    {
        [JsonProperty("@show_no")]
        public string show_no { get; set; }
        [JsonProperty("@show_datetime")]
        public string show_datetime { get; set; }
    }

public class Event
{
    [JsonProperty("@event_no")]
    public string event_no { get; set; }
    [JsonProperty("@name")]
    public string name { get; set; }
    public Show Show { get; set; }
}

public class Events
    {
        public List<Event> Event { get; set; }
    }

    public class EventsData
    {
        public Events Events { get; set; }
    }

但是,当我尝试反序列化时出现以下错误:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'VM_Models.Show' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

我到底做错了什么?如果我制作 Show 属性 dynamic 而不是 class Show 那么它就可以工作

更新:我确实在数据中发现有时会有不止一个节目:

{ "@event_no":"54535", "@name":"已编辑", “展示”: [ { "@show_no": "1", "@show_datetime": "2009-05-06 19:00:00" }, { "@show_no": "2", "@show_datetime": "2009-05-07 19:00:00" }, { "@show_no": "3", "@show_datetime": "2009-05-08 19:00:00" }, { "@show_no": "4", "@show_datetime": "2009-05-09 19:00:00" }, { "@show_no": "5", "@show_datetime": "2009-05-10 19:00:00" } ] }

问题是我从 XML 转换为 JSON 到对象。我决定从 XML 直接反对:

  [XmlRoot(ElementName = "Show", Namespace = "http://REDACTED/schema")]
            public class Show
            {
                [XmlAttribute(AttributeName = "show_no")]
                public string Show_no { get; set; }
                [XmlAttribute(AttributeName = "show_datetime")]
                public string Show_datetime { get; set; }
            }
    
            [XmlRoot(ElementName = "Event", Namespace = "http://REDACTED/schema")]
            public class Event
            {
                [XmlElement(ElementName = "Show", Namespace = "http:/REDACTED/schema")]
                public List<Show> Show { get; set; }
                [XmlAttribute(AttributeName = "event_no")]
                public string Event_no { get; set; }
                [XmlAttribute(AttributeName = "name")]
                public string Name { get; set; }

            }
    
            [XmlRoot(ElementName = "Events", Namespace = "http://REDACTED/schema")]
            public class Events
            {
                [XmlElement(ElementName = "Event", Namespace = "http://REDACTED/schema")]
                public List<Event> Event { get; set; }
                [XmlAttribute(AttributeName = "xmlns")]
                public string Xmlns { get; set; }
            }

然后在代码中:

XmlSerializer serializer = new XmlSerializer(typeof(Events));
TextReader reader = new StringReader(response.Content);
Events result = (Events)serializer.Deserialize(reader);

现在我可以正常查看所有数据属性了。