php operation JSON format data implementation code
- 2020-05-10 17:52:41
- OfStack
Knowledge:
1. Introduction to JSON data format
2. Encode the data into JSON format
3. Decode and operate JSON data
The data format of JSON is represented as follows:
Encoding data into JSON format with php:
It should be pointed out that Chinese characters cannot be encode under non-UTF-8 encoding, and the result will be null. Therefore, if you use gb2312 to write PHP code, then you need to use iconv or mb to UTF-8 to json_encode.
Output :(JSON format)
{"firstname":"\u975e\u8bda","lastname":"\u52ff\u6270","contact":{"email":"fcwr@ofstack.com","website":"http:\/\/www.ofstack.com"}}
Decode and process JSON data with php:
Output:
If you are the
Array ( [firstname] = > � � � [lastname] = > Hang hang [contact] = > stdClass Object ( [email] = > fcwr@ofstack.com [website] = > //www.ofstack.com ) )
1. Introduction to JSON data format
2. Encode the data into JSON format
3. Decode and operate JSON data
The data format of JSON is represented 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" }
] }
Encoding data into JSON format with php:
<?php
//php I'm going to represent it as an array JSON Format data
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', ' If you are the '),
'lastname' => iconv('gb2312', 'utf-8', ' You are the one '),
'contact' => array(
'email' =>'fcwr@ofstack.com',
'website' =>'//www.ofstack.com',
)
);
// The array is encoded as JSON The data format
$json_string = json_encode($arr);
//JSON Format data can be output directly
echo $json_string;
?>
It should be pointed out that Chinese characters cannot be encode under non-UTF-8 encoding, and the result will be null. Therefore, if you use gb2312 to write PHP code, then you need to use iconv or mb to UTF-8 to json_encode.
Output :(JSON format)
{"firstname":"\u975e\u8bda","lastname":"\u52ff\u6270","contact":{"email":"fcwr@ofstack.com","website":"http:\/\/www.ofstack.com"}}
Decode and process JSON data with php:
<?php
//php I'm going to represent it as an array JSON Format data
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', ' If you are the '),
'lastname' => iconv('gb2312', 'utf-8', ' You are the one '),
'contact' => array(
'email' =>'fcwr@ofstack.com',
'website' =>'//www.ofstack.com',
)
);
// The array is encoded as JSON The data format
$json_string = json_encode($arr);
// will JSON Format data is decoded, decoded is not JSON Data format, not available echo Direct output
$obj = json_decode($json_string);
// Force conversion to array format
$arr = (array) $obj;
// The data inside is called as an array
echo iconv('utf-8','gb2312',$arr['firstname']);
echo '</br>';
// Output array structure
print_r($arr);
?>
Output:
If you are the
Array ( [firstname] = > � � � [lastname] = > Hang hang [contact] = > stdClass Object ( [email] = > fcwr@ofstack.com [website] = > //www.ofstack.com ) )