Note. I sent a similar question that was the ancestor of this question, since I initially thought about using JSON.NET to parse JSON, but I'm using the built-in deserializer, so this is another question.
This is what I'm trying to do: for example, I have a class called Item. There are many βelementsβ in json (if that's what they are called, they mimic the Item class), and each of them contains 3 fields: an integer named id, a string named name and a datetime named creationTime. I would like to parse all of these "element" elements from json into a list of Item objects. I created 3 fields in the Item class to match JSON.
This is what I am doing now:
JavaScriptSerializer ser = new JavaScriptSerializer(); List<Item> items = ser.Deserialize<Item>(new StreamReader(response.GetResponseStream()).ReadToEnd());
However, this does not work because I "cannot implicitly convert the type" superapi.Item "to" System.Collections.Generic.List <superapi.Item > ". Thus, I do not know how to approach this problem, since JSON has a lot of Item architecture elements. Is it possible to do this in a foreach loop to find every deserialized element in JSON, add it to the list and continue the loop? I will try to do this with some kind of similar code and I will post my results.
Thanks!
UPDATE: Here is what JSON looks like:
[{ "Id": 1, "Name": "First item name", "creationTime": "\/Date(1247258293690)\/" }, { "Id": 2, "Name": "Second item name", "creationTime": "\/Date(1247088323430)\/" }]
This is my Item class as follows:
public class Item { public int Id { get; set; } public string Name { get; set; } public DateTime creationTime { get; set; } }
Maxim zaslavsky
source share