PHP json format and js json format js cross domain call implementation code
- 2020-05-24 05:16:45
- OfStack
So let's look at an js function
This function, the first alert(json[1].username); "candy" will be prompted. The json variable is an array object. So call it in a format like obj.username.
The second alert (json2 [0] [0]). "crystal" will be prompted. The json2 variable is fully in 1 json format. Both the json and json2 variables achieve the same effect, but json2 is significantly more streamlined than json.
This is json for JavaScript.
Let's take a look at the json format in php.
So let's look at one piece of code
In this code, $arr is an array. We use json_encode to convert $arr to json.
This 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 what php does with json data.
For json data, php can also convert json data into an array using the json_decode() function.
For example, in the above code, we use the json_decode function. It will print out the array above.
$jsonstr = json_encode($arr);
$jsonstr = json_decode($jsonstr);
print_r($jsonstr);
Next, let's look at how php json data and js json data call each other.
We create a new php_json.php file
The code is as follows:
PS: at the end of php_json.php file var jsonstr= < ? = $jsonstr ? > ; This one sentence. This is assigning data in json format to the jsonstr variable.
Let's create another json.html file
The code is as follows:
This way, when we look at json.html, loadjson(jsonstr) will prompt "program" and "climber"
This also implements js cross-domain calls.
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]);
}
This function, the first alert(json[1].username); "candy" will be prompted. The json variable is an array object. So call it in a format like obj.username.
The second alert (json2 [0] [0]). "crystal" will be prompted. The json2 variable is fully in 1 json format. Both the json and json2 variables achieve the same effect, but json2 is significantly more streamlined than json.
This is json for JavaScript.
Let's take a look at the json format in php.
So let's look at one piece of code
$arr = array (
array (
'catid' => '4',
'catname' => ' Love never fails ',
'meta_title' => ' Love never fails blog '
),
array (
'catid' => '6',
'catname' => 'climber',
'meta_title' => ' The climber ',
)
);
$jsonstr = json_encode($arr);
echo $jsonstr;
In this code, $arr is an array. We use json_encode to convert $arr to json.
This 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 what php does with json data.
For json data, php can also convert json data into an array using the json_decode() function.
For example, in the above code, we use the json_decode function. It will print out the array above.
$jsonstr = json_encode($arr);
$jsonstr = json_decode($jsonstr);
print_r($jsonstr);
Next, let's look at how php json data and js json data call each other.
We create a new php_json.php file
The code is as follows:
$arr = array (
array (
'catid' => '4',
'catname' => ' Love never fails ',
'meta_title' => ' Love never fails blog '
),
array (
'catid' => '6',
'catname' => 'climber',
'meta_title' => ' The climber ',
)
);
$jsonstr = json_encode($arr);
----- The following is written in the php Outside the range -----
var jsonstr=< ? = $jsonstr ? >;
PS: at the end of php_json.php file var jsonstr= < ? = $jsonstr ? > ; This one sentence. This is assigning data in json format to the jsonstr variable.
Let's create another json.html file
The code is as follows:
<SCRIPT type=text/javascript src="php_json.php"></SCRIPT><SCRIPT language=javascript type=text/javascript>
function loadjson(_json)
{
if(_json)
{
for(var i=0;i<_json.length;i++)
{
alert(_json[i].catname);
}
}
}
loadjson(jsonstr)
</SCRIPT>
This way, when we look at json.html, loadjson(jsonstr) will prompt "program" and "climber"
This also implements js cross-domain calls.