PHP, MYSQL & JSON
Ok, So I'm using AJAX to pull some information from my MYSQL database and put it onto the screen. The thing is I cannot understand JSON for the life of me. Can you suggest any tutorials or anything that will help?
I mean I get that I can encode my query via JSON but I guess it's the javascript side I do not un开发者_如何转开发derstand.
Here's my quick tutorial:
JSON is a means for expressing data for arrays, objects, and their contents. It has nothing to do with object behaviour (methods).
<?php
class Test {
public $hello = 'hello';
public $something = array('hello1', 'hello2');
public __construct() {
}
public void printHello() {
echo $this->hello;
}
}
?>
This class would in JSON would look like:
var obj = {
"hello": "hello",
"something": ["hello1", "hello2"]
};
As you can see, JSON is similar to maps in a lot of languages (key/value pairs). You can also see, that only data is represented. JSON is also shorthand for JavaScript builtins. For example, this previous object can be written in JavaScript like so.
var obj = new Object();
obj.hello = "hello";
obj.something = new Array("hello1", "hello2");
Hope this gives you a little idea of what JSON is about.
First, read this: http://www.json.org/js.html
Then, practice with this: http://jsonlint.com/
You can read about using JSON in JavaScript at the Mozilla docs.
Maybe this short example will help you: http://www.factsandpeople.com/facts-mainmenu-5/26-html-and-javascript/89-jquery-ajax-json-and-php.
jQuery docs - getJSON()
How to use JSON (updated with example)
http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/
精彩评论