1

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

3
  • 1
    What is the contents of ncontacts after the Deserialize call? Commented Sep 28, 2012 at 20:51
  • when i put a breakpoint on that line it shows that it's null Commented Sep 28, 2012 at 20:55
  • if it's null, then Deserialize didn't work. Do you get any errors? Commented Sep 28, 2012 at 21:02

1 Answer 1

2

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.

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

5 Comments

Wouldn't that depend on his locale?
how can i change the date string to fit the valid format for deserialization instead of changing the property?
@justnS No, then serialization would have to be made depending on the receivers locale.
It's now \"Bdate\" : \"Date(1348865672139)\" but still not working :/
@DanBarzilay Your string does not include /s. (you have problem while escaping the special chars.) Try exactly this string json = @"{""Name"":""dan"",""Age"":25,""City"":""lllal"",""About"":""im dan"",""Bdate"":""\/Date(1348866103586)\/""}";

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.