【问题标题】:JSON doesn't serialize correctlyJSON 没有正确序列化
【发布时间】:2016-03-23 23:38:06
【问题描述】:

我对 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","re​​sult":[{"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}

类类型:

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

【问题讨论】:

  • 只有 id 被序列化
  • rpc 来自哪里?
  • 这是来自 Betfair 的提要,它是这样来的
  • 对象不匹配 - 你有一个包装的 jsonrpc 对象你必须考虑。

标签: c# json serialization types json.net


【解决方案1】:

已解决,我必须创建另一个类 EventResult,它在深入研究 JSON 之前收集顶级数据

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

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

【讨论】:

    猜你喜欢
    • 2019-08-21
    • 2019-04-06
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2019-04-11
    相关资源
    最近更新 更多