Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.net deseralize to a list of objects in c# .net 2.0

I'm trying to deseralize some json into a collection (list), but I'm not sure which method will return a list of objects, or do I have to loop through something and copy it to my own list?

Can anyone tell me the syntax or method I should use for this.

I've created my object with some properties, so it's ready to be used to hold the data. (title,url,description)

I've tried this, but it doesn't seem quite right

 List<newsItem> test = (List<newsItem>)JsonConvert.DeserializeObject(Fulltext);
like image 410
Chris Barry Avatar asked Dec 21 '09 16:12

Chris Barry


1 Answers

Did you try looking at the help?

http://james.newtonking.com/json/help/?topic=html/SerializingCollections.htm

string json = @"[
  {
    ""Name"": ""Product 1"",
    ""ExpiryDate"": ""\/Date(978048000000)\/"",
    ""Price"": 99.95,
    ""Sizes"": null
  },
  {
    ""Name"": ""Product 2"",
    ""ExpiryDate"": ""\/Date(1248998400000)\/"",
    ""Price"": 12.50,
    ""Sizes"": null
  }
]";

List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);

Console.WriteLine(products.Count);
// 2

Product p1 = products[0];

Console.WriteLine(p1.Name);
// Product 1
like image 137
James Newton-King Avatar answered Oct 03 '22 18:10

James Newton-King