Knowledge Points:
1. Introduction to JSON data format
2. Encode the data into JSON format
3, decoding the JSON data, and operation
The JSON data format is represented in the following way:
Copy CodeThe code is as follows:
{"Programmers": [
{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},
{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},
{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}
],
"Authors": [
{"FirstName": "Isaac", "LastName": "Asimov", "Genre": "Science Fiction"},
{"FirstName": "Tad", "LastName": "Williams", "Genre": "Fantasy"},
{"FirstName": "Frank", "LastName": "Peretti", "Genre": "Christian Fiction"}
],
"Musicians": [
{"FirstName": "Eric", "LastName": "Clapton", "instrument": "Guitar"},
{"FirstName": "Sergei", "LastName": "Rachmaninoff", "Instrument": "Piano"}
] }
Encode the data in JSON format using PHP:
Copy CodeThe code is as follows:
Using arrays in PHP to represent JSON-formatted data
$arr = Array (
' FirstName ' = iconv (' gb2312 ', ' utf-8 ', ' Non-Prudential '),
' LastName ' = Iconv (' gb2312 ', ' utf-8 ', ' Don't Disturb '),
' Contact ' = = Array (
' Email ' = ' fcwr@jb51.net ',
' website ' = ' http://www.jb51.net ',
)
);
Encode an array into a JSON data format
$json _string = Json_encode ($arr);
JSON format data can be output directly
echo $json _string;
?>
It should be pointed out that under the non-UTF-8 encoding, the Chinese characters will not be encode, the result will be null, so if you use gb2312 to write PHP code, then you need to use the content of the language containing iconv or MB to UTF-8 and then Json_encode.
Output: (JSON format)
{"FirstName": "\u975e\u8bda", "LastName": "\u52ff\u6270", "contact": {"email": "Fcwr@jb51.net", "website": "http:\/\/ Www.jb51.net "}}
The JSON data is decoded and processed in PHP:
Copy CodeThe code is as follows:
Using arrays in PHP to represent JSON-formatted data
$arr = Array (
' FirstName ' = iconv (' gb2312 ', ' utf-8 ', ' Non-Prudential '),
' LastName ' = Iconv (' gb2312 ', ' utf-8 ', ' Don't Disturb '),
' Contact ' = = Array (
' Email ' = ' fcwr@jb51.net ',
' website ' = ' http://www.jb51.net ',
)
);
Encode an array into a JSON data format
$json _string = Json_encode ($arr);
Decoding JSON format data, not JSON data format after decoding, not using echo Direct output
$obj = Json_decode ($json _string);
Force conversions to an array format
$arr = (array) $obj;
Call the data inside the array mode
echo iconv (' Utf-8 ', ' gb2312 ', $arr [' FirstName ']);
Echo '
';
Output array Structure
Print_r ($arr);
?>
Output:
Non-Prudential
Array ([FirstName] = 闈 undercover 瘹 [LastName] = 鍕-carry stevioside [contact] = StdClass Object ([email] = fcwr@jb51.net [website] = http://www.jb51.net))
http://www.bkjia.com/PHPjc/324624.html www.bkjia.com true http://www.bkjia.com/PHPjc/324624.html techarticle Knowledge Points: 1, JSON data format Introduction 2, the data encoded into JSON format 3, the JSON data decoding, and the operation of the JSON data format is represented as follows: Copy the code as follows: {...