JSON is a subset of JavaScript object and array literals that can be used for data transmission. JSON is less complex than XML, and it can speed up performance because of its smaller file size than comparable XML documents.
JSON consists of two data structures:
-
q Objects—An unordered collection of name/value pairs
-
q Arrays—An ordered collection of values
There are no variables or other control structures in JSON. It’s designed solely for transmitting data.
JSON syntax is based on JavaScript syntax for object literals and array literals. When you use literals, you include the data itself, rather than an expression that generates the data. For example, in the following code block, the value that’s assigned to the variable x is a literal (15), while the value that’s assigned to the variable y is an expression (3*x) that must be evaluated before the assignment is complete.
var x = 15;
var y = 3 * x;
JSON Datatypes
JSON data structures can include any of the following types of data:
-
q String
-
q Number
-
q Boolean value (true/false)
-
q null
-
q Object
-
q Array
JSON strings must be double-quoted. They use standard JavaScript escape codes and add a backslash before the following characters:
-
q “ (quotation marks)
-
q b (backspace)
-
q n (new line)
-
q f (form feed)
-
q r (carriage return)
-
q t (horizontal tab)
-
q u (plus 4 digits for a Unicode character)
-
q \ (backslash)
-
q / (forward slash)
For example, to represent the string I feel “funny”, you escape both sets of quotation marks around the word funny:
"I feel \"funny\""
Object Literals
JavaScript objects can be defined using an object constructor or an object literal. To define a new object with a constructor, you use the Object constructor function with the new keyword.
var member = new Object();
Then you can add properties to the object using dot notation.
member.name = "James";
member.address = "32 Smith Rd";
member.isRegistered = true;
You can also add properties to the object using array syntax.
member["name"] = "James";
member["address"] = "325 Smith Rd";
member["isRegistered"] = true;
You can create the same object more efficiently by using an object literal.
var member =
{name: "James",
address: "32 Smith Road",
isRegistered: true
};
JSON doesn’t use constructors, only literals. The following code shows the member object formulated as JSON text:
{"name": "James",
"address": "32 Smith Road",
"isRegistered": true
}
Array LiteralsJavaScript arrays can also be created usinga constructor or using an array literal. To define a new array with a constructor, you use the Array constructor function with the new keyword.var myArray = new Array();Then, you can add members to the array using square brackets and an index value that indicates the position in the array.myArray[0] = 1218; myArray[1] = "1000" myArray[2] = "james";You can create the same object more efficiently using an array literal. var myArray = [1218, "Crawford", "Drive"]; The following code shows this array formulated as JSON text:[1218, "1000", "James"]Using a JSON ParserYou can use a JSON parser to create JSON text from objects and arrays, and to create objects and arrays from JSON text. A JSON parser is provided on the JSON site at http://www.json.org/json.js. You can copy this JavaScript file and reference it in your page by including this code in the head section of the page: <script type="text/javascript" src="json.js"></script> It defines two functions:toJSONString()parseJSON()The toJSONString() method is added to the JavaScript Object and Array definitions, and it enables you to convert a JavaScript object or array to JSON text. The object or array doesn't need to be preformulated as a literal in order to use this method. For example, the following code for the member object uses the Object constructor:<script type="text/javascript"> var member = new Object(); member.name = "James"; member.address = "32 Smith Rd"; member.isRegistered = true; member = member.toJSONString(); alert("The member object as a JSON data structure: \n" + member); </script>
Dear Author netprogramminghelp.com !
Very remarkable topic
I want to quote your post in my blog. It can?
And you et an account on Twitter?
I’ve been reading a few posts and i’m adding your blog to my rss reader , thanks ! acquisto case
Hello, I just joined and I am going to catch up by reading for a while. I hope I can join in soon. Digital 8 Camcorders
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
Heard about this site from my friend. He pointed me here and told me I’d find what I need. He was right! I got all the questions I had, answered. Didn’t even take long to find it. Love the fact that you made it so easy for people like me.
Aw, this was a really quality post. In theory I’d like to write like this too – taking time and real effort to make a good article… but what can I say… I procrastinate alot and never seem to get something done.
That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.
Hi there, awesome site. I thought the topics you posted on were very interesting
While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!
Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it’s nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.
Bloggers are not appreciated enough, many thanks for taking the time to post this.
I really enjoyed reading through this article.Thank you.
Please, will you PM me and inform me few many more is bothered this, My corporation is absolutely fan of the site…
I feel like you could probably teach a class on how to make a great blog. This is fantastic! I have to say, what really got me was your design. You certainly know how to make your blog more than just a rant about an issue. Youve made it possible for people to connect. Good for you, because not that many people know what theyre doing.