PHP operation JSON-formatted data implementation Code _php tutorial

Source: Internet
Author: User
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: {...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.