Example of PHP output JSON format data

Source: Internet
Author: User
Tags json php and

We have to use Content-type:text/json to display the JSON data properly.

The code is as follows Copy Code

<?php

Header (' Content-type:text/json '); This is the point, it tells the object receiving the data this page output is JSON data;

$json ={"n": "Name", "P": "Password"}; Although this line of data is in the form of JSON format, it is not processed as JSON data without the above sentence;

Echo $json;

?>

Json_encode Of course you can also enter the JSON string, see below for a few examples.

The function that PHP generates JSON is: Json_encode ($PHPcode);
The function of PHP parsing JSON is: Json_decode ($JSONcode);

So there are many forms of JSON, and different forms are different in the form of PHP interpretation.
//Form 1: A completely object form in which the data is called an associated array in JavaScript, unlike a generic array, it can be accessed using a string as an index (with "[]" or "." To represent the hierarchy)

  code is as follows copy code

$json = ' {' Item1 ': {"Item11": {"n": "Chenling", "M": "Llll"}, "Sex": "Www.111cn.net", "Age": "A"}, "item2": {"Item21": "Ling", "Sex": "Female", "Age": "24"}} ';
$J =json_decode ($json);
Print_r ($J);    
Output:
StdClass Object
(
[item1] => stdClass Object
(
[ITEM11] => StdClass Object
(
[n] => chenling
[m] => llll
)

[sex] => www.111cn.net
[age] => 2 5
)

[item2] => stdClass Object
(
[item21] => ling
[sex] => women
[age] => ;
)

For example, if I'm going to get the attribute that the value is chenling, you should access it like this:
$J->item1->item11->n;//This gets the value of the property N: chenling
In fact, this type of access is similar to accessing normal object properties, and it is equivalent to accessing a 3-D array.

Form 2: Object and array blending

  code is as follows copy code
$json = ' {"item1": [{"Name": [{"Chen": "Chenling", "Ling": "Chenli"}], "Sex": "Male" , "Age": "},{" "Name": "Sun", "Sex": "Female", "Age": "24"}]} ';
$J =json_decode ($json);
Print_r ($J);
Output:
StdClass Object
(
[item1] => Array
(
[0] => StdClass object
(
[          Name] => Array
(
[0] => stdClass Object
(
[Chen] => chenling
[Ling] => Chenli
)

)

[sex] => man
[age] =>
)

[ 1] => stdClass Object
(
[name] => Sun
[sex] => female
[age] =>
)

)

For example, if I'm going to get the element that the value is chenling, you should access it like this:
$J->item1[0]->name[0]->chen;//This will get the element Chen's value: chenling
In fact, this form of access combines the access of objects and arrays, as well as accessing a 5-D array.

Form 3: Complete array form

The code is as follows Copy Code

$json = ' [[item1 ', ' item11 '],[' n ', ' chenling '],[' m ', ' llll ']] ';
$J =json_decode ($json);
Print_r ($J);
Will output:
Array
(
[0] => Array
(
[0] => item1
[1] => ITEM11
)

[1] => Array
(
[0] => N
[1] => chenling
)

[2] => Array
(
[0] => m
[1] => llll
)
)

For example, if I'm going to get the element that the value is chenling, you should access it like this:
$J [0][1];//This will get the element value chenling the element
But there is a disadvantage in this way is that the string can not be indexed, only with numbers, in the form of a complete object to solve this problem
In fact, this form of access is the way the array is accessed, equivalent to accessing a 2-D array.

Summary:
As you can see from the example above, JSON is a bit like XML, and it can be used to transfer data with structure between PHP and JavaScript.

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.