PHP json format and js json format js cross-domain call implementation code_PHP tutorial

WBOY
Release: 2016-07-21 15:15:43
Original
755 people have browsed it

First look at a js function

Copy code The code is as follows:

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

var json2 = [['crystal','20'],['candy','24']];
alert(json2[0][0]);
}

In this function, the first alert(json[1].username); will prompt "candy". The json variable is an array object. So it needs to be called in the format of obj.username.
The second alert(json2[0][0]); will prompt “crystal”. The json2 variable is a complete json format. Both json and json2 variables achieve the same effect, but json2 is obviously much more streamlined than json.
This is JavaScript’s json format.
Let’s take a look at the json format in php.
Let’s look at a piece of code first
Copy the code The code is 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, we use json_encode to change $ arr is converted to json format.
This code will output:

[{"catid":"4","catname":"u7a0bu7a0b","meta_title":"u7a0bu7a0bu535au5ba2"},{"catid":"6" ,"catname":"climber","meta_title":"u6500u767bu8005"}]
This is how PHP handles json data.
For json data, PHP can also use the json_decode() function to convert json data into an array.
For example, in the above code, we use the json_decode function to process it. The above array will be printed out again.
$jsonstr = json_encode($arr);
$jsonstr = json_decode($jsonstr);
print_r($jsonstr);
Next, let’s take a look at php json data and js json data How to call each other.

We create a new php_json.php file

The code is as follows:
Copy the code The code is 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);
-----The following is written outside the php range-----
var jsonstr=< ? = $jsonstr ? >;

PS: At the end of the php_json.php file, var jsonstr=< ? = $jsonstr ? >; This sentence. This is to assign json format data to the jsonstr variable.
Let’s create another json.html file

The code is as follows:
Copy the code The code is as follows:



In this way, when we view json.html, loadjson(jsonstr) will Tip "Chengcheng" and "climber"
This also realizes js cross-domain calling.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326029.htmlTechArticleFirst look at a js function copy code. The code is as follows: function jsontest() { var json = [{'username': 'crystal','userage':'20'},{'username':'candy','userage':'24'}]; alert(json[1].username)...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!