需要帮助使用 C# javascriptserializer 反序列化 json 字符串

Need help deserializing json string with C# javascriptserializer

我正在尝试在 C# 中反序列化以下 json 字符串,但它无法正常工作。

下面的代码returns计数为0。我不确定我做错了什么。

JavaScriptSerializer ser = new JavaScriptSerializer();
Addresses addresses = ser.Deserialize<Addresses>(json);

我的JSON是:

{
  "addresses":[
{
  "first_name":"Sarah",
  "last_name":"Halawani",
  "line1":"1653 OCEAN PKWY",
  "company":"",
  "city":"BROOKLYN",
  "state":"NY",
  "subscriber_id":null,
  "country_name":"United States",
  "country_abbreviation":"USA",
  "latitude":"40.6085",
  "longitude":"-73.9675",
  "verified":true
},
{
  "first_name":"Jean",
  "last_name":"Mizrahi",
  "line1":"1733 OCEAN PKWY",
  "company":"",
  "city":"BROOKLYN",
  "state":"NY",
  "subscriber_id":null,
  "country_name":"United States",
  "country_abbreviation":"USA",
  "latitude":"40.6065",
  "longitude":"-73.9671",
  "verified":true
}
  ]
}

我的 类 是:

public class Addresses
{
    public List<Address> address { get; set; }
    public Addresses() { address = new List<Address>(); }
}

public class Address
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string line1 { get; set; }
    public string company { get; set; }
}

您拼错了 属性 address。它应该是 addresses 以匹配 JSON 属性 名称:

public class Addresses
{
    public List<Address> addresses { get; set; }
    public Addresses() { addresses = new List<Address>(); }
}

为避免此类错误,请考虑使用 code-generation 工具,例如 Visual Studio 中的 http://json2csharp.com/ or Paste JSON as Classes,然后手动删除不需要的属性。