Can you explain it in most simple words?
Best with a demo script.
Can you explain it in most simple words?
Best with a demo script.
JSON is Javascript source code that creates a data structure.
JavaScript Object Notation, is a lightweight computer data interchange format. More info here.
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.
A short but good descriptionUnderstanding JSON: the 3 minute lesson
Another is here JSON for Dummies (like me) Part 1
For more info JSON Tutorials
And please look into Introducing JSON
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.
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.)
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.
eg. Lets 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 doublequotes right.
Her's some examples in Java. Shown are two different API implementations.
import flexjson.JSONSerializer;
import net.sf.json.JSONObject;
...
// This sequence will convert a superclass to a targeted subclass.
// This is needed to add special business logic and add meta methodology.
// Here securityFiling was read from the database via hibernate.
// JSONObject would have missed many of the deeper methods since it's all lazy loaded.
String sfJson = new JSONSerializer().deepSerialize( securityFiling );
// Now convert to JSONObject and prepare for subclassing
JSONObject jsonSecurityFiling = JSONObject.fromObject( sfJson );
// Now subclass and assign all values, set by reflection
BusinessSecurityFiling bsf = ( BusinessSecurityFiling ) JSONObject.toBean( jsonSecurityFiling, BusinessSecurityFiling.class );
JSON can do some pretty heavy lifting. :)