Json is widely used in program data interaction due to its efficient performance. This article will give you a brief analysis of the encoding and decoding of json in php and js. To give you a reference, I hope it will be helpful to everyone.
php in
1)Encoding
$jsonStr = json_encode($array)
2)Decoding
$arr = json_decode($jsonStr)
<?php echo json_encode("中文", JSON_UNESCAPED_UNICODE);
Add parameters: JSON_UNESCAPED_UNICODE.
Test environment:
PHP Version 5.5.36
js中
1. Encoding
var str = obj.toJSONString();//需要JSON解析器或工具类
2. Three types of decoding:
var jsonStr = ''[{"id":"1","name":"computer"}]"; 1) var obj = eval(jsonStr); //兼容最好 2) var obj = jsonStr.parseJSON(); //这俩需要浏览器带JSON解析器。或者加一个json的工具类 3) var obj = JSON.parse(jsonStr);//
Related recommendations:
##A brief analysis of the problem of json data transmitted from Ajax background success
Detailed explanation of json format control in php
Detailed explanation of php catching specific types of exceptions
The above is the detailed content of A brief analysis of json transmission in php and js. For more information, please follow other related articles on the PHP Chinese website!