This project has moved and is read-only. For the latest updates, please go here.

Json C#

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


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": "",
  "description": "BitTorrent Search > All > ubuntu",
  "language": "en-us",
  "category": "All",
  "ttl": 60,
  "image": {"title": "isoHunt > All > ubuntu",
  "url": "",
  "link": "",
  "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":"","guid":"","enclosure_url":"", "length":"732912681", "type":"application/x-bittorrent", "category":"Apps", "original_site":"", "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.