Skip to content

Instantly share code, notes, and snippets.

@bmakarand2009
Created September 15, 2012 01:37
Show Gist options
  • Save bmakarand2009/3725990 to your computer and use it in GitHub Desktop.
Save bmakarand2009/3725990 to your computer and use it in GitHub Desktop.
javascript-json
/*
http://jsfiddle.net/mbhatamb/F9rGz/
Standardized and defined by Douglas Crockford (www.json.org),
Advandates
- lightweight
- fast
- readable
- natural to javascript object
where as XML is verbose. Douglas says, any dumb can create a better format than XML.
Other disadv - xml processing is very time consuming, lot of overhead.
Note : JSON.stringify() and JSON.parse() methods. JSON is supported by all Major browsers except IE7.
*/
//e.g of a object literal object
/*
Below is a javascript object in literal notation
*/
var rss = {
channel: {
version: "2.0",
description: "sample rss feed",
title: " my sample title",
items: [
{
link: "http://mysample.com",
itemdesc: "sample item desc",
price: 20},
{
link: "http://mysample2.com",
itemdesc: "item2 sample desc",
price: 50}
]
}
}
console.log(rss.channel.version);
console.log(rss.channel.items[1].link);
console.log(rss.channel.items[1].price);
//JSON Demo
var myjsonstr = JSON.stringify(rss);
//typically you will get the myjsonstr from a AJAX call to Server
console.log("JSON String is" + myjsonstr);
var myrssobj = JSON.parse(myjsonstr);
console.log(myrssobj.channel.version);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment