PHPJSON format data interaction instance code details _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php array to json php json
Detailed description of PHPJSON format data interaction instance code. JSON parsing in PHP mainly uses two PHPJSON functions: json_encode and json_decode, which are much more convenient than parsing XML in PHP. The following describes how to use PHPJSON. Basic Introduction to JSON (JavaScri mainly uses two php json Functions, json_encode and json_decode, to parse JSON in PHP, which is much more convenient than PHP. The following describes the use of php json.
Basic Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format.

JSON has two structures:

Set of "name/value" pairs, which can be understood as associative array in PHP ).

An ordered list of values ). In PHP, it can be understood as an ordinary array (array ).

An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.

An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas. Value can be a string, number, true, false, null, object, or array enclosed by double quotation marks ). These structures can be nested.

My current understanding of JSON is more biased towards arrays. similar to the associated arrays in PHP, you can convert PHP arrays to JSON format.

For more information about JSON, refer to the JSON official website and IBM's introduction to JSON.

  Php json parsing instance

At the beginning of PHP5.2, JSON has been used as part of the PHP extension, so you do not need to install JSON separately. In addition, to ensure that the following php json instance does not contain Chinese garbled characters, make sure that your encoding format is UTF8.

JSON is a part of Javascript. First, let's take a look at how to define JSON in Javascript.

The code is as follows:


Var json_obj = {'webname': 'php website '};
Alert (json_obj.WebName );
Var json_arr = [{'webname': 'php website', 'Website': 'http: // www.jb51.net'}, {'arttitle ': 'php JSON instance description'}]
Alert (json_arr [1]. ArtTitle );
Var php_json = [['php website development tutorial net', 'http: // www.jb51.net'], ['Article title', 'php JSON instance'];
Alert (php_json [1] [0]);


In this JSON instance code, I constructed all the JSON formats,
Row 3 defines that JSON exists as an object
Row 3 defines JSON as an array and nested JSON object. Therefore, it is similar to PHP's associated array, but it is still an object.
Row 3, indicating that JSON exists as a normal array.
Note: Because Javascript is case sensitive, pay attention to the case sensitivity of keys when accessing JSON-associated array objects.
When PHP interacts with JSON data, the first step is to convert the PHP array to JSON data. you can use the json_encode function that comes with PHP5, when PHP parses the transmitted JSON format data, it needs to use the json_decode function for parsing and converting to a PHP array. The example code for PHP to convert and parse JSON data is as follows:

The code is as follows:


$ Json_arr = array ('webname' => 'php website', 'Website' => 'http: // www.jb51.net ');
$ Php_json = json_encode ($ json_arr );
Echo $ php_json;
$ Php_json = json_decode ($ php_json );
Print_r ($ php_json );
?>


Description: The associated array is defined in PHP, and then converted to JSON format data through json_encode. the converted JSON object can be seen through the echo statement, using the JSON parsing function json_decode of PHP, you can parse JSON data into an associated array of PHP. If the encoding is incorrect, garbled characters will appear in JSON Chinese. If you use Editplus to Debug PHP, note that when saving the file, the code will change ANSI to UTF8.

Finally, let's take a look at the complete php json interaction instance.

The code is as follows:


$ Json_arr = array ('webname' => 'php website', 'Website' => 'http: // www.jb51.net ');
$ Php_json = json_encode ($ json_arr );
?>

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.