【问题标题】:Deserializing JSON Object containing another object反序列化包含另一个对象的 JSON 对象
【发布时间】:2016-10-18 11:37:47
【问题描述】:

我正在尝试使用 Newtonsoft.Json 库反序列化这个 JSON 字符串。但是返回的反序列化对象总是返回 null。我认为这与播放器对象中的地址对象有关。

这是 JSON 字符串

{  
   "player":{  
      "id":"ed704e61-f92b-4505-b087-8a47ca4d1eaf",
      "firstName":"Jack",
      "lastName":"Russel",
      "nickname":"Barky",
      "dateOfBirth":"1995-08-16T00:00:00",
      "sex":"m",
      "address":{  
         "street":"Elmstreet",
         "number":"5",
         "alphaNumber":"",
         "poBox":"",
         "postalCode":"90001",
         "city":"Los Angeles",
         "country":"United States"
      },
      "email":[  
         "barky@dog.com",
         "barky@mydogpension.com"
      ],
      "phone":[  
         "0123 45 67 89 10"
      ]
   },
   "requestReference":2000,
   "requestStatus":"Request OK",
   "requestDetails":null
}

这些是 RootObject、Player 和 Address 类。它是 RootObject 的 Player 对象,它不断为上面的 JSON 字符串返回一个空值。因此,在调用 offcourse 时会抛出空引用异常:

public class RootObject
{
    public Player player { get; set; }
    public int requestReference { get; set; }
    public string requestStatus { get; set; }
    public string requestDetails { get; set; }
}    

public class Address
{
    public string street { get; set; }
    public string number { get; set; }
    public string alphaNumber { get; set; }
    public string poBox { get; set; }
    public string postalCode { get; set; }
    public string city { get; set; }
    public string country { get; set; }
}

public class Player
{
    public Guid id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string nickname { get; set; }
    public DateTime dateOfBirth { get; set; }
    public string sex { get; set; }
    public Address address { get; set; }
    public List<string> email { get; set; }
    public List<string> phone { get; set; }
}

这是用于反序列化的代码行:

RootObject playerRoot = JsonConvert.DeserializeObject&lt;RootObject&gt;(_the_json_string_shown_above);

【问题讨论】:

  • 您的代码适用于 Json.Net 7
  • 所有反序列化对我来说都很好 - 只是逐字测试了您的代码。使用 Newtonsoft.Json 版本 9.0.1

标签: c# json json.net deserialization


【解决方案1】:

我使用的是 Newtonsoft.Json 8.0.2.19309。 我必须在 Player 类中的 Address 对象中添加一个 JsonProperty 属性。然后对象被反序列化就好了。

public class Player
{
    public Guid id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string nickname { get; set; }
    public DateTime dateOfBirth { get; set; }
    public string sex { get; set; }
    [JsonProperty]
    public Address address { get; set; }
    public List<string> email { get; set; }
    public List<string> phone { get; set; }
}

【讨论】:

  • 那么您使用的是什么版本的 Newtonsoft.Json?
  • Newtonsoft.Json 8.0.2.19309 是该项目使用的。
  • 很奇怪,如果没有[JsonProperty] 属性,它不会反序列化,但它与我的和@heinzbeinz 的属性配合得很好。不过,很高兴你最终让它工作了 :) 编码愉快!
猜你喜欢
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
相关资源
最近更新 更多