0

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; }
}
2
  • 1
    does you json variable contains data? you haven't included that part of the code Commented Feb 16, 2017 at 19:04
  • This was fixed by correcting a spellling mistake. Commented Feb 21, 2018 at 14:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dbc! That did it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.