PHP JSON format and JS JSON format JS cross-domain call to implement code _php skills

Source: Internet
Author: User
Tags php json
First look at a JS function
Copy Code code as follows:

function Jsontest ()
{
var json = [{' username ': ' crystal ', ' userage ': '},{' username ': ' Candy ', ' userage ': ' 24 '}];
alert (json[1].username);

var json2 = [[' Crystal ', '],[' Candy ', ' 24 ']];
Alert (json2[0][0]);
}

This function, the first alert (json[1].username); Will prompt "Candy". A JSON variable is an array object. So you want to use a format such as Obj.username to invoke.
The second alert (json2[0][0]); Will prompt "Crystal". The Json2 variable is a full JSON format. Both JSON and JSON2 variables have the same effect, but json2 is significantly leaner than JSON.
This is the JSON format for JavaScript.
Now let's look at the JSON format in PHP.
Or look at a piece of code first.
Copy Code code as follows:

$arr = Array (
Array (
' CatID ' => ' 4 ',
' CatName ' => ' Chengcheng ',
' Meta_title ' => ' Chengcheng blog '
),

Array (
' CatID ' => ' 6 ',
' CatName ' => ' climber ',
' Meta_title ' => ' climber ',
)
);
$jsonstr = Json_encode ($arr);
Echo $jsonstr;


In this code, $arr is an array, and we use Json_encode to convert $arr to JSON format.
This piece of code will output:

[{"CatID": "4", "CatName": "\u7a0b\u7a0b", "Meta_title": "\u7a0b\u7a0b\u535a\u5ba2"},{"catid": "6", "CatName": " Climber "," Meta_title ":" \u6500\u767b\u8005 "}]
This is how PHP handles JSON data.
For JSON data, PHP can also use the Json_decode () function to convert JSON data to an array.
For example, in the above code, we use the Json_decode function to deal with. The above array is also printed out.
$jsonstr = Json_encode ($arr);
$jsonstr = Json_decode ($JSONSTR);
Print_r ($JSONSTR);
Next, let's look at how the PHP JSON data and the JS JSON data are invoked with each other.

We create a new php_json.php file

The code is as follows:
Copy Code code as follows:

$arr = Array (
Array (
' CatID ' => ' 4 ',
' CatName ' => ' Chengcheng ',
' Meta_title ' => ' Chengcheng blog '
),

Array (
' CatID ' => ' 6 ',
' CatName ' => ' climber ',
' Meta_title ' => ' climber ',
)
);
$jsonstr = Json_encode ($arr);
-----is written below the PHP range-----
var jsonstr=<? = $jsonstr? >;

PS: Var jsonstr=< at end of php_json.php file? = $jsonstr? >; This sentence. This is to assign the data in JSON format to the JSONSTR variable.
We'll build a json.html file.

The code is as follows:
Copy Code code as follows:

<script type=text/javascript src= "php_json.php" ></script><script language=javascript Javascript>
function Loadjson (_json)
{
if (_json)
{
for (Var i=0;i<_json.length;i++)
{
alert (_json[i].catname);
}
}
}

Loadjson (JSONSTR)
</SCRIPT>

In this way, when we look at the json.html, Loadjson (jsonstr) prompts "Chengcheng" and "climber".
This also implements the JS cross domain call.

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.