Dan Barzilay
Dan Barzilay

Reputation: 4983

C# JavaScriptSerializer json array deserialization

Code:

string json = "[{\"Name\" : \"dan\", \"Age\" : 25, \"City\" : \"lllal\", \"About\" : \"im dan\", \"Bdate\" : \"26/06/1997\"}]";

JavaScriptSerializer ser = new JavaScriptSerializer();
List<Person> ncontacts = ser.Deserialize<List<Person>>(json);

foreach (Person person in ncontacts)
    listView1.Items.Add(person.Name);

Person Class:

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string City { get; set; }
    public string About { get; set; }
    public DateTime Bdate { get; set; }
}

The problam is that the listview stays with 0 items, even that it's supposed to have dan.

I've tryed to debug and i put a breakpoint on the foreach line, what's wierd is that it never gets to that line.. if i put a breakpoint one line before it breaks..

Any help would be welcome, Dan

Upvotes: 1

Views: 2965

Answers (1)

L.B
L.B

Reputation: 116118

Your date string 26/06/1997 is not in a valid format for deserialization and your code gets exception. If you replace public DateTime Bdate { get; set; } with public string Bdate { get; set; } you can see that it is working.

Upvotes: 2

Related Questions