Tuesday, December 2, 2014

Json structure

JSON  is JavaScript Object Notation
Its human readable text to transmit Data objects.

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}
The same text expressed as XML:
< menu id="file" value="File">
  < popup>
    < menuitem onclick="CreateNewDoc()" value="New">
    < menuitem onclick="OpenDoc()" value="Open">
    < menuitem onclick="CloseDoc()" value="Close">
  < /menuitem>
< /popup> < /menu>

1. And this can be accessed by javascript by calling JSON.parse(
var p = JSON.parse(menu);
2. Calling JSON from Jquery by using $.getJSON
$.getJSON( "ajax/test.json", function( data )
{
  var items = [];
  $.each( data, function( key, val ) 
  {
    items.push( "< li id='" + key + "'>" + val + "</ li>" );
  });
  $( "< ul/>", 
  {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "body" );
}
);

Ref:https://jquery.org/

No comments:

Post a Comment