PHP JSON Format Data Interactive instance code detailed _php tips

Source: Internet
Author: User
Tags php array to json php json php web development php website
Parsing json in PHP is mainly used to Json_encode and Json_decode two PHP JSON functions, more convenient than PHP parsing XML, the following detailed introduction of the use of PHP json.
JSON Basics Introduction

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

There are two main structures of JSON:

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

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

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

An array is an ordered collection of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma) value. Values (value) can be enclosed in double quotes (string), numeric (number), True, False, NULL, objects (object), or arrays (array). These structures can be nested.

My current understanding of JSON is more of an array, similar to an associative array in PHP, where 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 Parse Instance

PHP5.2 began to have JSON as part of the PHP extension, so you don't need to install JSON separately. In addition, in order to ensure that the following PHP JSON instances do not appear in Chinese garbled, please ensure that your coding format is UTF8.

JSON is part of JavaScript, first we look at how to define JSON in JavaScript

Copy Code code as follows:

var json_obj = {' WebName ': ' PHP Web Development Tutorial Network '};
Alert (json_obj. WebName);
var json_arr =[{' WebName ': ' PHP Web Development tutorial Web ', ' WebSite ': ' http://www.jb51.net '},{' arttitle ': ' PHP json instance '} '
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 built all the JSON forms,
Line 1th, which defines the existence of JSON as an object
Line 4th, which defines JSON as an array and nested JSON objects, is somewhat analogous to an associative array of PHP, but is still an object.
Line 7th, the JSON is set to exist as a normal array.
Note: Because JavaScript is case-sensitive, you should be aware of the capitalization of the key when you access 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 used when PHP parses the JSON-formatted data passed Json_ The decode function is parsed into a PHP array, and the instance code that PHP transforms and parses the JSON data is as follows
Copy Code code as follows:

<?php
$json _arr = Array (' WebName ' => ' PHP Web Development Tutorial Network ', ' 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 : This defines the associative array through PHP, and then converts it to JSON-formatted data via Json_encode, and the Echo statement lets you see the converted JSON object and parse the function via PHP json json_ Decode can parse JSON-formatted data into an associative array of PHP. If the encoding is not correct, JSON will appear garbled in Chinese. If you use EditPlus to debug PHP, note that the encoding changes ANSI to UTF8 when you save the file.

Finally, let's look at the complete PHP JSON interaction instance

Copy Code code as follows:

<?php
$json _arr = Array (' WebName ' => ' PHP Web Development Tutorial Network ', ' WebSite ' => ' http://www.jb51.net ');
$php _json = Json_encode ($json _arr);
?>
<script type= "Text/javascript" >
var Php_json = <?= $php _json?>;
</script>
<script type= "Text/javascript" >
function Php_json_dis (Php_json)
{
Alert (Php_json. WebName);
Alert (Php_json. WebSite);
}
Php_json_dis (Php_json);
</script>

At this point, PHP through the Json_encode and Json_decode functions of the JSON format data conversion and resolution of the example is finished.

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.