Querying The Archivist API With JSON.NET

December 16 2011

Someone recently asked me for a sample of how to query The Archivist API to programmatically get a list of the top users of a given search term.

So I put a quick sample together, using the most excellent JSON.NET library’s LINQ provider for querying JSON. 

I used an archive on Wittgenstien.  To get the JSON that generated this chart, I simply append ?format=json to the URL, like this: http://archivist.visitmix.com/irhetoric/75/user?format=json  which results in the following response:

[{"Date":"\/Date(-62135596800000+0000)\/","Count":667.0,"Name":"lucasofri"},{"Date":"\/Date(-62135596800000+0000)\/","Count":439.0,"Name":"3PennyMovies"},{"Date":"\/Date(-62135596800000+0000)\/","Count":326.0,"Name":"quotemeal"},{"Date":"\/Date(-62135596800000+0000)\/","Count":260.0,"Name":"SiegenerZeitung"},{"Date":"\/Date(-62135596800000+0000)\/","Count":237.0,"Name":"lucasarrimada"},{"Date":"\/Date(-62135596800000+0000)\/","Count":236.0,"Name":"Double_bliss"},{"Date":"\/Date(-62135596800000+0000)\/","Count":175.0,"Name":"praashok"},{"Date":"\/Date(-62135596800000+0000)\/","Count":139.0,"Name":"PhilosophyQuotz"},{"Date":"\/Date(-62135596800000+0000)\/","Count":135.0,"Name":"NihilDeNada"},{"Date":"\/Date(-62135596800000+0000)\/","Count":132.0,"Name":"wilsonvoight"},{"Date":"\/Date(-62135596800000+0000)\/","Count":126.0,"Name":"imamrasyidi"},{"Date":"\/Date(-62135596800000+0000)\/","Count":117.0,"Name":"Iceburner"},{"Date":"\/Date(-62135596800000+0000)\/","Count":112.0,"Name":"emilsantosiii"},{"Date":"\/Date(-62135596800000+0000)\/","Count":111.0,"Name":"unwetterwarner"},{"Date":"\/Date(-62135596800000+0000)\/","Count":104.0,"Name":"LogicalAnalysis"},{"Date":"\/Date(-62135596800000+0000)\/","Count":102.0,"Name":"macondo83"},{"Date":"\/Date(-62135596800000+0000)\/","Count":93.0,"Name":"de_340513031"},{"Date":"\/Date(-62135596800000+0000)\/","Count":90.0,"Name":"neofibot"},{"Date":"\/Date(-62135596800000+0000)\/","Count":86.0,"Name":"hw1"},{"Date":"\/Date(-62135596800000+0000)\/","Count":83.0,"Name":"wirsiegen"},{"Date":"\/Date(-62135596800000+0000)\/","Count":82.0,"Name":"souvik_rt"},{"Date":"\/Date(-62135596800000+0000)\/","Count":79.0,"Name":"asmiather"},{"Date":"\/Date(-62135596800000+0000)\/","Count":76.0,"Name":"ogallardov"},{"Date":"\/Date(-62135596800000+0000)\/","Count":72.0,"Name":"pr0ject2501"},{"Date":"\/Date(-62135596800000+0000)\/","Count":71.0,"Name":"ggrigoriadis"}]

 

Here’s the program I wrote to parse this:

string json = string.Empty; using (WebClient webClient = new WebClient()) { json = webClient.DownloadString("http://archivist.visitmix.com/irhetoric/75/user?format=json"); } JArray result = JArray.Parse(json); foreach (JObject user in result) { Console.WriteLine((string)user["Name"] + " - " + (float)user["Count"]); } Console.ReadLine();

You can see how I simply pass the JSON to the static .Parse method hanging off the JArray class. If the root of the JSON wasn’t an array, I would have used the JObject.Parse() method. Once it is in the array, I can loop it and extract the values. You can also write LINQ queries using JSON.NET. For example, if I only wanted users whose tweet count was greater than 100, I could write this:

var greaterThan100 = from r in result where (float)r["Count"] > 100 select r; foreach (JObject user in greaterThan100) { Console.WriteLine((string)user["Name"] + " - " + (float)user["Count"]); }

Pingbacks and trackbacks (1)+

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading