JSON没有正确序列化

时间:2016-03-23 23:38:07

标签: c# json serialization types json.net

我对JSON非常陌生,但是试图让以下内容工作但只有ID序列化,我怀疑我需要以某种方式更深入地嵌入字符串:

字符串:

{\"jsonrpc\":\"2.0\",\"result\":[{\"event\":{\"id\":\"27727330\",\"name\":\"Germany U21 v Faroe Islands U21\",\"countryCode\":\"DE\",\"timezone\":\"Europe/London\",\"openDate\":\"2016-03-24T19:00:00.000Z\"},\"marketCount\":24}],\"id\":1}

{" jsonrpc":" 2.0""导致":[{"事件" {" ID&# 34;:" 27727330"," name":" Germany U21 v Faroe Islands U21"," countryCode":" DE&# 34;,"时区":"欧/伦敦"" Opendate里":" 2016-03-24T19:00:00.000Z"} " marketCount" 24}]," ID":1}

班级类型:

public class EventListing
    {

    [JsonProperty(PropertyName = "id")]
    public string id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string name { get; set; }

    [JsonProperty(PropertyName = "countryCode")]
    public string countryCode { get; set; }

    [JsonProperty(PropertyName = "timezone")]
    public string timezone { get; set; }

    [JsonProperty(PropertyName = "marketCount")]
    public string marketCount { get; set; }

    [JsonProperty(PropertyName = "openDate")]
    public DateTime? openDate { get; set; }
}

有问题的代码:

EventListing Test = Newtonsoft.Json.JsonConvert.DeserializeObject<EventListing>(theStringAbove);

1 个答案:

答案 0 :(得分:0)

修复了,我不得不创建另一个类EventResult,它在深入JSON之前收集顶级数据

    public class EventResult
    {
    [JsonProperty(PropertyName = "event")]
    public Event Event { get; set; }

    [JsonProperty(PropertyName = "marketCount")]
    public int MarketCount { get; set; }
    }