简体   繁体   中英

Need help deserializing json string with C# javascriptserializer

I'm trying to deserialize the following json string in C# but it's not working correctly.

The following code returns a count of 0. I'm not sure what I'm doing wrong.

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

My JSON is:

{
  "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
}
  ]
}

And my classes are:

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

You misspelled the property address . It should be addresses to match the JSON property name:

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

To avoid errors like this, consider using a code-generation tool such as http://json2csharp.com/ or Paste JSON as Classes in Visual Studio, and then manually removing unwanted properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM