by jason15. December 2008 23:59JavaScript Object Notation (JSON) is one of the technologies used by AJAX and other "Web 2.0" technologies. It is a way to communicate in an object-oriented fashion between the web server and a client-side, JavaScript-based application. Every now and again, you might find yourself in a situation where you would need to consume a JSON service, but without a JavaScript endpoint. I ran into this situation when I needed to talk to an application that exposed a web interface, but has no other API hook. My mind first jumped to "screen scraping" the web pages in the exposed web interface. However, when I started to dive into the HTML, I found a very rich JSON service behind those pages.
An example message that I saw coming back from the application looked like this:
{"build":12639,"label": [
["done",8]
]
,"torrents": [
["74C61EB07A63ED2CBC84B8ECCFF85B1222A8006E",136,"myfile.iso",366779378],
["B28240047FD6C6A7219663AA862B2F1F4DD8AE24",136,"yourfile.iso",366784662]]
,"torrentc": "884739139"}
Examining the message, you'll notice that it seems to be broken into key-value pairs. For instance, the variable named "build" has a value of 12639. More complex object shapes can be represented by arrays like the value of the "label" variable. The above message could be represented in the C# class below:
More...