Can you explain 开发者_开发技巧it in most simple words?
Best with a demo script.
JSON is a way of sharing data (usually between the browser and a server).
JavaScript allows for two way to store collections of values:
//arrays:
[value, value, value]
//objects:
{key:value, key:value, key:value}
At some point, a guru known as Doug realized that it is usually most efficient to send data to JavaScript already setup like an object. [Rather than PHP sending a comma-delimited strings, post-data, XML, or even HTML, all of which have to be painstakingly parsed by the JavaScript].
So he called that idea JSON, wrote up a spec for it, and the standard was born.
For example, let's say your login.php script should return the users name, total posts, and days since registered:
//XML
"<xml..><details>\
<user>Jim</user><posts>239</posts><since>Jan09</since>\
</details>"
//POSTData
"user=Jim&posts=239&since=Jan09"
//JSON
"{user:'Jim', posts:239, since:'Jan09'}"
The last one can be easily parsed by JS (using eval
), and the details can be used intuitively:
var user = details.user;
EDIT:
It was correctly noted that to be valid JSON, all strings must be double quoted.
This was done to prevent JS from croaking on reserved keywords (in JS one may not use certain words, such as class
, unless they are quoted. So {class:'mike'}
cannot be used).
It should also be pointed out that PHP 5.2+ has functions which can be used to create or parse JSON:
<?php
$arr = array ('a'=>'ay','b'=>'bee','c'=>'cee');
echo json_encode($arr); //outputs {"a":"ay","b":"bee","c":"cee"}
?>
The json_decode
function will ONLY work if the JSON is valid, so it is sometimes important to get those double-quotes right.
It's basically a way of describing objects in text - a text-based serialization format. However, the beauty of it is that it's also just normal JavaScript. The syntax of JavaScript allows objects to be initialized in a pretty concise format, and that format is fairly easy to generate/parse in other languages too.
So, you get "built-in" deserialization in JavaScript (i.e. you can just interpret the text as code1) with no extra libraries, and other platforms can create it, usually with a library. (Typically a web server will generate JSON for a browser to interpret.)
1 This assumes you trust your data source completely, of course - executing arbitrary text as code is pretty dangerous from a security standpoint.
JSON is Javascript source code that declares a data structure, typically sent by a web server to a browser. The browser runs the code through the normal javascript parser and a data structure pops out.
A Javascript declaration could look like:
var myvar = {"column1": "valuie1"};
^^^^^^^^^^^^^^^^^^^^^^
The part underlined with ^^^ is what became known as JSON.
So early on, some Javascript would grab a text from a server and parse it like:
var myvar = eval('(' + textfromserver + ')');
Since eval is dangerous, it's nowadays more often used like:
var myObject = JSON.parse(myJSONtext);
It's a hack so many people found useful they made it a standard. See this wikepedia page for a much more thorough explanation.
JSON is Javascript source code that creates a data structure.
I'll try to make it simple. If you're familiar with XML at all, it's like XML in principle in that it stores data in an easy-to-read manner for both humans and programs. It is labeled as a "data interchange format" because you will see it used as an intermediary to move data between one program and another.
For example, you might have certain database that you would like others to take information from and use in their own program. Rather than giving them full access to the database, you might restrict their access by writing some sort of JSON layer. People can then access JSON much like they might with a RSS feed. Real-life example: Yahoo provides a JSON layer for their search engine so people can write desktop widgets (or whatever else) that can run yahoo searches and get results sent directly to the desktop widgets.
The ugly alternative to using something like JSON might be to have your program get the HTML contents of a web page and somehow find the information you needed. (And if the HTML layout of the website changes, you have to change your program.)
If you want to see things practically do watch this video which demonstrates JSON Click here to see the JSON video
JSON ( Javascript object notation ) is a simple data exchange format which helps to communicate between JavaScript and server side technologies like Servlets , JSP , WCF , ASP.Net etc.
The format of JSON is as shown below. The below format represents a “Customer” object with “CustomerCode” and “CustomerName” property.
![{"CustomerCode":"1001","CustomerName":"Shiv"}][2]
Now this format can be easily consumed by javascript and transformed in to javascript object. Look at the above figure where we have provided JSON to javascript variable and you can see how a javascript object is evaluated with a “CustomerCode” and “CustomerName” property.
If you look at the web architecture it has two parts browser and server. On the browser side Javascript is the most prominent and well established language while on server side you have different technologies like JSP, ASP.NET, PHP etc.
So if the server side technologies emit out JSON format which can be easily transformed in to a javascript object that would really ease the communication between multiple server side technologies and javascript language.
JSON(JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way objects
are built in JavaScript).
JSON is a way of serializing in such a way, that it becomes JavaScript code. When executed (with eval or otherwise), this code creates and returns a JavaScript object which contains the data you serialized. This is available because JavaScript allows the following syntax:
var MyArray = [ 1, 2, 3, 4]; // MyArray is now an array with 4 elements
var MyObject = {
'StringProperty' : 'Value',
'IntProperty' : 12,
'ArrayProperty' : [ 1, 2, 3],
'ObjectProperty' : { 'SubObjectProperty': 'SomeValue' }
}; // MyObject is now an object with property values set.
You can use this for several purposes. For one, it's a comfortable way to pass data from your server backend to your JavaScript code. Thus, this is often used in AJAX.
You can also use it as a standalone serialization mechanism, which is simpler and takes up less space than XML. Many libraries exists that allow you to serialize and deserialize objects in JSON for various programming languages.
Here is a very quick and concise write-up. http://secretgeek.net/json_3mins.asp
(thnx to mson, Vilx)
精彩评论