Question

Both "array" and "objects" may be not quite the proper terminology here, but I'm sure you got my continental drift.

I see this example for serializing/deserializing a custom object from the official docs:

 product.Name = "Apple";
 product.ExpiryDate = new DateTime(2008, 12, 28);
 product.Price = 3.99M;
 product.Sizes = new string[] { "Small", "Medium", "Large" };

 string output = JsonConvert.SerializeObject(product);
 //{
//  "Name": "Apple",
//  "ExpiryDate": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

But I need to deserialize an "array" of objects-as-JSON elements, something like:

IEnumerable<Platypus> deserializedProduct = JsonConvert.DeserializeObject<Platypus>(output);

...or:

List<Platypus> deserializedProduct = JsonConvert.DeserializeObject<Platypus>(output);

What is needed on the right side, the JsonConvert/JSON.NET side, to accomplish this?

Was it helpful?

Solution

Why not something like this:

List<Product> deserializedProduct = JsonConvert.DeserializeObject<List<Product>>(object);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top