Home > Backend Development > PHP Tutorial > A brief analysis of json transmission in php and js

A brief analysis of json transmission in php and js

*文
Release: 2023-03-18 22:28:02
Original
1542 people have browsed it

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)
Copy after login

2)Decoding

$arr = json_decode($jsonStr)
Copy after login
<?php
echo json_encode("中文", JSON_UNESCAPED_UNICODE);
Copy after login


Add parameters: JSON_UNESCAPED_UNICODE.

Test environment:

PHP Version 5.5.36

js中

1. Encoding

var str = obj.toJSONString();//需要JSON解析器或工具类
Copy after login

2. Three types of decoding:

var jsonStr = &#39;&#39;[{"id":"1","name":"computer"}]";
1) var obj = eval(jsonStr); //兼容最好
2) var obj = jsonStr.parseJSON(); //这俩需要浏览器带JSON解析器。或者加一个json的工具类
3) var obj = JSON.parse(jsonStr);//
Copy after login

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!

Related labels:
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