25 September 2010

Getting data from JSON with jQuery is really simple.


Here is a commented example of a simple script that appends div with data from JSON.
Source code viewer
  1. <script type="text/javascript">
  2. // this gets JSON data from an url
  3. $.getJSON("/json-data-for-parsing",
  4. // this function gets called when data has been recieved
  5. function(data){
  6. // parsing JSON data, line by line(like foreach)
  7. $.each(data, function(i,item){
  8. // puts all titles in our div
  9. $("#test").append(item.title+"<br />");
  10. });
  11. });
  12. </script>
  13. <div id="test"></div>
Programming Language: Javascript
You can do anything with that data for an example put it into a table, list or even generate rss feeds from it.
Parsing JSON:
JSON data is converted to object. If you have more complex JSON data and you don't know how objects work then you can find some help from: json.org - Parsing JSON in JavaScript.