|
網誌存檔
熱門網志
|
|
Filed: Tue, Apr 10 2007 under Programming|| Tags: JSON javascript objects ajaxWidely hailed as the successor to XML in the browser, JSON aspires to be nothing more than a simple, and elegant data format for the exchange of information between the browser and server; and in doing this simple task it will usher in the next version of the World Wide Web itself. The Object: An IntroductionBehold, an Object… var myFirstObject = {}; It may not look like much, but those squiggly braces have the potential to record every bit of information humanity has ever gathered, and express the most complex programs computer scientists can dream up. In fact, Javascript itself is stored inside a set of squiggly braces just like that, as are all of its primitive data types -- strings, numbers, arrays, dates, regular expressions, they`re all objects and they all started out just like myFirstObject. Creating A New ObjectThe old way to create a new object was to use the var myJSON = new Object(); This method has been deprecated now in favor of simply defining an empty object with squigly braces… var myJSON = {}; Objects as DataAt its most base level a Javascript Object is a very flexible and robust data format expressed as var myFirstJSON = { "firstName" : "John", This object has 3 properties or name/value pairs. The name is a string -- in our example, firstName, lastName, and age. The value can be any Javascript object (and remember everything in Javascript is an object so the value can be a string, number, array, function, even other Objects) -- In this example our values are John, Doe, and 23. John and Doe are strings but age is a number and as you can see this is not a problem. This data format is called JSON for JavaScript Object Notation. What makes it particularly powerful is that since the value can be any data type, you can store other arrays and other objects, nesting them as deeply as you need. Here is an example of a somewhat complex JSON structure… var employees = { "accounting" : [ // accounting is an array in employees. Here employees is an object. That object has two properties or name/value pairs. Accounting is an array which holds two JSON objects showing the names and age of 2 employees. Likewise sales is also an array which holds two JSON objects showing the name and ago of the two employees who work in sales. All of this data exists within the employees object. There are several different ways to access this data. Accessing Data In JSONThe most common way to access JSON data is through dot notation. This is simply the object name followed by a period and then followed by the name/property you would like to access. var myObject = { `color` : `blue` }; If your object contains an object then just add another period and name… var myObject = { `color` : `blue`, Using the document.writeln(employees.sales[0].firstName + ` ` + employees.sales[0].lastName); We can also access the second person who works in document.writeln(employees.accounting[1].firstName + ` ` + employees.accounting[1].lastName); To recap, the Simulating An Associative ArrayYou can also access JSON data as if it were an Associative Array. var myFirstJSON = { "firstName" : "John", Be aware that this is NOT an associative array, however it appears. If you attempt to loop through myFirstObject you will get, in addition to the three properties above, any methods or prototypes assigned to the object, so while you`re more than free to use this method of addressing JSON data, just treat it for what it is (Object Properties) and not for what it is not (Associative Array). Receiving JSON via AJAXThere are three seperate ways to receive JSON data via AJAX. Assignment, Callback, and Parse. JSON Via AssignmentThere`s no standard naming convention for these methods, however var JSONFile = "someVar = { `color` : `blue` }"; // example of what is received from the server. JSON Via CallbackThe second method calls a pre-defined function and passes the JSON data to that function as the first argument. A good name for this method is the function processData(incommingJSON) { JSON Via ParseThe third and final method sends a raw object which must be parsed by a function. This could be referred to as the // The following block implements the string.parseJSON method As you can see, you`ll need to include the public domain prototype which will parse the JSON data, however once it`s included, processing JSON data is as simple as it looks in the last three lines of the above example. Of the three extraction methods, the Retrieving JSON Data Via AjaxFor these examples we`ll be using the Ajax framework from the article: The Ultimate Ajax Object. When you are communicating with your own servers, AJAX is one of the better ways to transfer data from the server to the browser. Provided the server is under your control you can be reasonably assured this method of data transfer is safe. Here is an example of a simple AJAX request which communicates with the server, retrieves some data, and passes it along to a processing function. We`ll be using the callback method here where the JSON object will execute a pre-defined function after it`s loaded, in this case -- processData(JSONData); function processData(JSONData) { In this example our data file, because it`s actually javascript code, when passed through the eval statement will execute processData, passing our actual JSON data to the function as the first argument. The next example will use the parse method and assumes you have the parseJSON prototype located elsewhere in your code. function processData(JSONData) { Now when the the server returns the JSON file, it`s parsed on the line which reads Howto Send JSON Data To The ServerBecause AJAX data is sent as an encoded string, some preparation of JSON data must be made before it can be sent to the server. Fortunately, Douglas Crockford at JSON.org has released a set of very useful routines which will convert any Javascript data type into a JSON string which can be easily sent to the server. The source for this library can be obtained at http://www.JSON.org/JSON.js. The code is public domain and is as easy to use as clipping the data and pasting it into your toolbox. The following example defines a JSON object then uses the toJSONString() method to convert the object into a string which is ready to be sent to the server. var employees = { "accounting" : [ // accounting is an array in employees. This outputs: //Outputs: Retreiving JSON From Third Party ServersThis section comes with a bunch of warnings. Up until this point, JSON and AJAX has been relatively secure since you are communicating with servers under your control, receiving data that is under your control. With third party services all of this changes. As of Internet Explorer 7 and Firefox 2, use of third party JSON data exposes your web page to malicious attacks and great security risks. The moment you request third party data you have given up control of your web page and the third party can do whatever they want with it from scraping the data and sending back sensitive information to installing listeners to wait for sensitive data to be entered. For most trusted sources the only thing an external JSON request will get you is a bunch of useful data. However if the page making the third party request contains form elements, requires a secure/encrypted connection, or contains ANY personal or confidential data you absolutely, positively should NOT use third-party JSON calls on that page. Before you attempt to use third party JSON data take a good, hard look at the page you are creating and now imagine that the worst sort of person is looking at that page with you. If you are uncomfortable with what that other person can do with the information displayed on that page, you should not be using third-party JSON calls. Quite a few services are starting to offer JSON in addition to RSS/XML as data formats. Yahoo, in particular, is quite progressive at implementing JSON. What`s very cool about JSON is that the web page can directly request and process that data, unlike XML which must be requested by the server and then passed along to the browser. Unfortunately there is no RSS/FEED standard for JSON, though most services try to emulate RSS XML in a JSON structure. For the following example we`ll use this site`s JSON feed, a simple one-to-one conversion of XML to JSON. The feed is even generated by the exact same program which generates the XML version, only the arguments passed on the URL determine which format is sent. The URL for this site`s feed is: http://www.hunlock.com/feed.php?format=JSON&callback=JSONFeed All third party JSON requests use the callback method where, once the JSON file has been loaded it will call a specific function, passing the JSON data as the first argument. Most services have a default function name they will call, and some services will allow you to change the name of the function which is called. My JSON feed will call JSONFeed() by default but you can change this by changing the name in the URL. If you changed the URL to read… http://www.hunlock.com/feed.php?format=JSON&callback=processJSON …now when the feed has been loaded it will call the function named All third party JSON requests are loaded via the <script> tag, just like you would use to load external javascript files. What`s a little different is that we create the script tag manually and attach it to the page manually as well. This is a fairly simple process however and the code to do it is quite simple and readable. Here`s a small function which will accept a url and load it. function loadJSON(url) { This function gets the (first) <head> element of our page, creates a new script element, gives it a type, sets the src attribute to the url which is passed and then appends the new script element to the page <head> Once appended, the script will be loaded and then executed. Here is a very simple little program to get this site`s JSON feed and display the items. function loadJSON(url) { |
-------------------------------------------------
| 上一篇:Understanding JSON: the 3 minute lesson | 下一篇:The Ultimate Ajax Object |

