Operations
What is JSON?
JSON is a data exchange format used to transmit excessive data in an interconnected network. Although XML exchange is dominant in the Internet, JSON is simpler and suitable for lightweight data.
Although JSON was originally invented by JavaScript and used to access remote data, it is now widely used in various languages, because JSON is a platform-independent data format.
JSON data types and instances
JSON supports various data types, including numbers, strings, boolean values, array data, and even object data (a set. Each element is in the form of a key: value pair, separated by commas, and use braces ).
Let's look at a simple example of JSON data. This example shows the details of an employee:
| The code is as follows: |
Copy code |
{"Id": "1 & Prime;," name ":" mike "," country ":" usa "," office ": [" microsoft ", "oracle"]} |
How to create and parse JSON data using PHP
PHP versions later than 5.2.0 provide JSON extensions to process JSON data. Through PHP, there are two functions: json_encode () and json_decode, which are very convenient for JSON data conversion and parsing.
First, let's look at a piece of PHP code that creates the above various JSON types using arrays:
| The code is as follows: |
Copy code |
$ Json_data = array ('id' => 1, 'name' => "mike", 'Country' => 'USA ', "office" => array ("microsoft", "oracle"); echo json_encode ($ json_data ); |
This code directly generates JSON data. Now let's use PHP to decode the above JSON:
| The code is as follows: |
Copy code |
$ Json_string = '{"id": 1, "name": "mike", "country": "usa", "office": ["microsoft ", "oracle"]} '; $ obj = json_decode ($ json_string ); |
Now the variable $ obj contains JSON data parsed using PHP. You can use the following methods to output and access the data:
| The code is as follows: |
Copy code |
Echo $ obj-> name; // displays mike echo $ obj-> office [0]; // displays microsoft |
You guessed it was correct. $ obj-> office is an array. You can use the foreach method of PHP to traverse it:
| The code is as follows: |
Copy code |
Foreach ($ obj-> office as $ val) echo $ val; |
Ajax returns JSON data
The original data is in JSON format.
The following example is from
| The code is as follows: |
Copy code |
$ (Function (){ $ ('# Send'). click (function (){ $. GetJSON ('test. Js', function (data ){ $ ('# ResText'). empty (); Var html = ''; $. Each (data, function (commentIndex, comment ){ Html + = '<div class = "comment"> }) ('{Restext'{.html (html ); }) }) })
What you need to do is to store the data in a. json or. js file in the correct format. The following is an example of the data transmitted in json format. [ { "Username": "Zhang San ", "Content": "sofa ." }, { "Username": "Li Si ", "Content": "bench ." }, { "Username": "Wang Wu ", "Content": "floor ." } ]
|
Php output JSON format
So how does php output the json format? Php uses the json_encode function, and jQuery uses datatype: json? Its output is as follows:
Obviously not what you want. Or a string. How do I implement it? In fact, it is very easy to add the following code to the PHP file header:
| The code is as follows: |
Copy code |
Header ('content-type: text/json ');
|
This header indicates that the output type of this file is json. The most common form we see is the verification code-php output verification image. Sometimes php can output css files, js files and other interesting things. Okay. Let's test it.
| The code is as follows: |
Copy code |
<? Php Header ('content-type: text/json '); $ Fruits = array ( "Fruits" => array ("a" => "orange", "B" => "banana", "c" => "apple "), "Numbers" => array (1, 2, 3, 4, 5, 6 ), "Holes" => array ("first", 5 => "second", "third ") ); Echo json_encode ($ fruits ); ?> |