PHP JSON Format Data Interaction Instance code detailed _php tutorial

Source: Internet
Author: User
Parsing JSON in PHP mainly uses Json_encode and Json_decode two PHP JSON functions, more convenient than PHP parsing XML, the following details the use of PHP json.
Introduction to JSON Basics

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

There are two main structures of JSON:

A collection of name/value pairs that can be understood in PHP as associative arrays (associative array).

The sequence of values (an ordered list of values). It can be understood as a normal array in PHP.

An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).

An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values. The value (value) can be a string enclosed in double quotation marks (string), a numeric value (number), True, false, NULL, an object, or an array. These structures can be nested.

My current understanding of JSON is more of an array, similar to the associative array in PHP, and you can convert the PHP array to JSON format.

For more information on JSON, please refer to the JSON website and IBM's introduction to JSON

  PHP JSON parsing example

PHP5.2 has started to use JSON as part of the PHP extension, so there is no need to install JSON separately. In addition, in order to ensure that the following PHP JSON instance does not appear in Chinese garbled, please ensure that your encoding format is UTF8.

JSON is part of JavaScript, first we look at how JSON is defined in JavaScript
Copy CodeThe code is as follows:
var json_obj = {' WebName ': ' PHP Web Development Tutorial Net '};
Alert (json_obj. WebName);
var json_arr =[{' WebName ': ' PHP Web Development Tutorial Network ', ' WebSite ': ' http://www.jb51.net '},{' arttitle ': ' PHP JSON instance explained '}]
Alert (json_arr[1). Arttitle);
var Php_json = [[' PHP Web Development Tutorial Net ', ' http://www.jb51.net '],[' article title ', ' PHP JSON instance explained ']];
Alert (php_json[1][0]);

In this JSON instance code I built all the JSON forms,
Line 1th, which defines the existence of JSON as an object
Line 4th, which defines JSON as an array, nested JSON objects, so something similar to the associative array of PHP, is still an object.
In line 7th, JSON is set to exist as a normal array.
Note: Because JavaScript is case-sensitive, it is important to note the case of key when accessing the JSON associative array object.
When PHP interacts with JSON-formatted data, the first step is to convert the PHP array into JSON-formatted data, using the PHP5-Json_encode function, which is required when PHP parses the JSON-formatted data passed in Json_ The decode function is parsed into a PHP array, and the instance code for PHP to convert and parse the JSON data is as follows
Copy CodeThe code is as follows:
$json _arr = Array (' WebName ' = ' PHP Web development Tutorial Net ', ' 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 associative array is defined here by PHP, and then converted to JSON-formatted data via Json_encode, and through the Echo statement you can see the converted JSON object through the PHP JSON parsing function json_ Decode can parse data in JSON format into an associative array of PHP. If the encoding is incorrect, the JSON Chinese will appear garbled. If you use EditPlus to debug PHP, be aware that the encoding changes ANSI to UTF8 when you save the file.

Finally, let's take a look at the full PHP JSON interaction instance
Copy the Code code as follows:
$json _arr = Array (' WebName ' = ' PHP Web development Tutorial Net ', ' WebSite ' + ' http://www.jb51.net ');
$php _json = Json_encode ($json _arr);
?>



At this point, the PHP json_encode and Json_decode functions of the JSON format data conversion and parsing of the example is finished.

http://www.bkjia.com/PHPjc/322847.html www.bkjia.com true http://www.bkjia.com/PHPjc/322847.html techarticle parsing json in PHP mainly uses Json_encode and Json_decode two PHP JSON functions, more convenient than PHP parsing XML, the following details the use of PHP json. JSON Basics Introduction to JSON (JAVASCRI ...

  • 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.