Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing some JSON with JSON.NET
    text
    copied!<p>I'm very new to JSON, and I need to parse some that an API is providing. A quick google search turned up <a href="http://james.newtonking.com/pages/json-net.aspx" rel="nofollow noreferrer"><strong>JSON.NET</strong></a>, so I'm trying to use it now to parse this JSON into a list object. First of all, <strong>is JSON.NET the best library to use</strong> for this?</p> <p>This is what I'm trying to do: I have a <strong>class called Item</strong>, for example. The json has many "elements" (if that's what they are called), and each <strong>contains 3 fields</strong>: 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 <strong>into a list of Item objects</strong>. I have created 3 fields in the Item class to match the JSON. How can this be done using JSON.NET? </p> <p>I've tried:</p> <pre><code>List&lt;Item&gt; fav = (List&lt;Item&gt;)new JsonSerializer().Deserialize(new JsonReader((TextReader)new StreamReader(response.GetResponseStream()))); </code></pre> <p>but it doesn't seem to work. I get a casting error - it just can't process it into a list enclosure, but I'm not even sure whether it's able to process the JSON into one Item class (JSON.NET is not very well-documented, but I'm going to heavily debug it tomorrow).</p> <p>Can you give me some sample code to explain how I can parse it with JSON.NET?</p> <p>Thanks!</p> <p><strong>UPDATE:</strong> By the way, forgot to mention - my project is going to be targeting .NET Framework 2.0, so I'm using the legacy version of JSON.NET: 1.3.1. Are there any HUGE advantages that may make the project worth converting to .NET 3.5, while undermining the minimal system requirements?</p> <p><strong>UPDATE #2:</strong> I have decided to use the JavascriptSerializer class in System.Web.Extensions.dll instead of JSON.NET, and a question about that is posted <a href="https://stackoverflow.com/questions/1450513/using-system-web-script-serialization-javascriptserializer-to-deserialize-json">here</a>. Thanks!</p>
 

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