Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)
    text
    copied!<p>I know there are a few posts about Newtonsoft so hopefully this isn't exactly a repeat...I'm trying to convert JSON data returned by Kazaa's API into a nice object of some kind</p> <pre><code>WebClient client = new WebClient(); Stream stream = client.OpenRead("http://api.kazaa.com/api/v1/search.json?q=muse&amp;type=Album"); StreamReader reader = new StreamReader(stream); List&lt;string&gt; list = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;List&lt;string&gt;&gt;(reader.Read().ToString()); foreach (string item in list) { Console.WriteLine(item); } //Console.WriteLine(reader.ReadLine()); stream.Close(); </code></pre> <p>That JsonConvert line is just the most recent one I was trying...I'm not quite getting it and was hoping to eliminate some footwork by asking you guys. I was originally trying to convert it into a Dictionary or something...and actually, I just need to snag a couple of values in there so judging by the documentation, maybe Newtonsoft's LINQ to JSON might be a better choice? Thoughts/Links?</p> <p>Here is an example of the JSON return data:</p> <pre><code>{ "page": 1, "total_pages": 8, "total_entries": 74, "q": "muse", "albums": [ { "name": "Muse", "permalink": "Muse", "cover_image_url": "http://image.kazaa.com/images/69/01672812 1569/Yaron_Herman_Trio/Muse/Yaron_Herman_Trio-Muse_1.jpg", "id": 93098, "artist_name": "Yaron Herman Trio" }, { "name": "Muse", "permalink": "Muse", "cover_image_url": "htt p://image.kazaa.com/images/54/888880301154/Candy_Lo/Muse/Candy_Lo-Muse_1.jpg", "i d": 102702, "artist_name": "\u76e7\u5de7\u97f3" }, { "name": "Absolution", "permalink": " Absolution", "cover_image_url": "http://image.kazaa.com/images/65/093624873365/Mus e/Absolution/Muse-Absolution_1.jpg", "id": 48896, "artist_name": "Muse" }, { "name": "Ab solution", "permalink": "Absolution-2", "cover_image_url": "http://image.kazaa.com/i mages/20/825646911820/Muse/Absolution/Muse-Absolution_1.jpg", "id": 118573, "artist _name": "Muse" }, { "name": "Black Holes And Revelations", "permalink": "Black-Holes-An d-Revelations", "cover_image_url": "http://image.kazaa.com/images/66/093624428466/ Muse/Black_Holes_And_Revelations/Muse-Black_Holes_And_Revelations_1.jpg", "id": 48813, "artist_name": "Muse" }, { "name": "Black Holes And Revelations", "permalink": "Bla ck-Holes-And-Revelations-2", "cover_image_url": "http://image.kazaa.com/images/86/ 825646911486/Muse/Black_Holes_And_Revelations/Muse-Black_Holes_And_Revelations_1 .jpg", "id": 118543, "artist_name": "Muse" }, { "name": "Origin Of Symmetry", "permalink": "Origin-Of-Symmetry", "cover_image_url": "http://image.kazaa.com/images/29/825646 912629/Muse/Origin_Of_Symmetry/Muse-Origin_Of_Symmetry_1.jpg", "id": 120491, "artis t_name": "Muse" }, { "name": "Showbiz", "permalink": "Showbiz", "cover_image_url": "http: //image.kazaa.com/images/68/825646182268/Muse/Showbiz/Muse-Showbiz_1.jpg", "id": 60444, "artist_name": "Muse" }, { "name": "Showbiz", "permalink": "Showbiz-2", "cover_imag e_url": "http://image.kazaa.com/images/50/825646912650/Muse/Showbiz/Muse-Showbiz_ 1.jpg", "id": 118545, "artist_name": "Muse" }, { "name": "The Resistance", "permalink": "T he-Resistance", "cover_image_url": "http://image.kazaa.com/images/36/825646864836/ Muse/The_Resistance/Muse-The_Resistance_1.jpg", "id": 121171, "artist_name": "Muse" } ], "per_page": 10 } </code></pre> <hr> <p>I did some more reading and found Newtonsoft's LINQ to JSON is exactly what I wanted...using WebClient, Stream, StreamReader, and Newtonsoft...I can hit Kazaa for JSON data, extract a URL, download the file, and do it all in like seven lines of code! I love it.</p> <pre><code>WebClient client = new WebClient(); Stream stream = client.OpenRead("http://api.kazaa.com/api/v1/search.json?q=muse&amp;type=Album"); StreamReader reader = new StreamReader(stream); Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine()); // Instead of WriteLine, 2 or 3 lines of code here using WebClient to download the file Console.WriteLine((string)jObject["albums"][0]["cover_image_url"]); stream.Close(); </code></pre> <hr> <p>This post gets so many hits I thought it might be helpful to include the "using" bits that are discussed in the comments.</p> <pre><code>using(var client = new WebClient()) using(var stream = client.OpenRead("http://api.kazaa.com/api/v1/search.json?q=muse&amp;type=Album")) using (var reader = new StreamReader(stream)) { var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine()); Console.WriteLine((string) jObject["albums"][0]["cover_image_url"]); } </code></pre>
 

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