Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing System.Web.Script.Serialization.JavascriptSerializer to deserialize JSON - how to?
    primarykey
    data
    text
    <p><em>Note: I posted <a href="https://stackoverflow.com/questions/1448184/deserializing-some-json-with-json-net">a similar question</a>, which was the ancestor of this question, as I was originally thinking of using JSON.NET to parse the JSON, but I'm using the built-in deserializer, so it's a different question.</em></p> <p>This is what I'm trying to do: I have a class called Item, for example. The json has many "elements" (if that's what they are called - they mimic the Item class), and each contains 3 fields: an integer named id, a string named name, and a datetime named creationTime. I would like to parse all of these Item "elements" from the json into a list of Item objects. I have created 3 fields in the Item class to match the JSON. </p> <p>This is what I'm currently doing: </p> <pre><code>JavaScriptSerializer ser = new JavaScriptSerializer(); List&lt;Item&gt; items = ser.Deserialize&lt;Item&gt;(new StreamReader(response.GetResponseStream()).ReadToEnd()); </code></pre> <p>However, this isn't working, because I "cannot implicitly convert type 'superapi.Item' to 'System.Collections.Generic.List<code>&lt;superapi.Item</code>>'". Thus, I do not know how to approach this problem, as there are many elements of the Item architecture in the JSON. Is it somehow possible to do this in a foreach loop, to find each deserialized Item in the JSON, add that to the list, and continue the loop? I'm going to attempt to do just that with some similar code, and I will post my results.</p> <p>Thanks!</p> <p><strong>UPDATE:</strong> This is what some of the JSON looks like:</p> <pre><code>[{ "Id": 1, "Name": "First item name", "creationTime": "\/Date(1247258293690)\/" }, { "Id": 2, "Name": "Second item name", "creationTime": "\/Date(1247088323430)\/" }] </code></pre> <p>This is what my Item class looks like:</p> <pre><code>public class Item { public int Id { get; set; } public string Name { get; set; } public DateTime creationTime { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload