Json C#

May 23, 2009 at 2:31 PM
Edited May 23, 2009 at 2:33 PM

Hey,

At first I kindly ask you not to tell me to go to google, I have wasted five hours on it already:

I have the following JSON data

 {"title": "isoHunt > All > ubuntu",
  "link": "http://isohunt.com",
  "description": "BitTorrent Search > All > ubuntu",
  "language": "en-us",
  "category": "All",
  "ttl": 60,
  "image": {"title": "isoHunt > All > ubuntu",
  "url": "http://isohunt.com/img/buttons/isohunt-02.gif",
  "link": "http://isohunt.com/",
  "width": 157,
  "height": 45},
"lastBuildDate": "Sat, 23 May 2009 13:24:16 GMT","pubDate": "Sat, 23 May 2009 13:24:16 GMT","total_results": 689, "items": {"list": [{"title":"Applications UNIX | ubuntu-9.04-desktop-i386 iso","link":"http://isohunt.com/torrent_details/77699397/ubuntu?tab=summary","guid":"http://isohunt.com/torrent_details/77699397/ubuntu?tab=summary","enclosure_url":"http://isohunt.com/download/77699397/ubuntu.torrent", "length":"732912681", "type":"application/x-bittorrent", "category":"Apps", "original_site":"http://thepiratebay.org/", "size":"698.96 MB", "files":"1","Seeds":"2514", "leechers":"170", "downloads":"38", "votes":"4","comments":"3","pubDate":"Thu, 23 Apr 2009 05:12:36 GMT"}]}}

From this i only need "link" string field and then output like Console.WriteLine(link);

I am using C# and Visual Studio 2008

I really need example, I am so freaked out with this deserializations and readers, so please understand me

Thanks a lot!

May 26, 2009 at 5:23 PM

It's very simple what you're trying to do. First things first, you need to deserialize your Json to a C# Object and then you can access the property like this:

MyObject  myObject = (MyObject)JsonConvert.DeserializeObject(myJsonString, typeof(MyObject));

//Accessing the top level title

string topTitle = myObject.title;

//Accessing the low level title in the items collection. I assume you have only one, other you need to loop through to get all available links.

string lowTitle = myObject.items.list[0].title;

You need to build a class that reflects all the Json properties that you have, so good luck with that.

StarFahd