Home > Article > Backend Development > How to convert json to string in php
With the rapid development of Internet technology, data interaction has become more and more common. In this context, JSON, as a lightweight data exchange format, has gradually become the first choice of many developers. In PHP language, we can easily convert data types such as arrays and objects into JSON format strings to facilitate data communication.
1. PHP converts arrays to JSON
In PHP, we can use the json_encode function to convert arrays into JSON strings. The method of using the function is as follows:
mixed json_encode(mixed $value[, int $options = 0[, int $depth = 512]]);
Among them, the $value parameter indicates the array or object that needs to be converted into a JSON format string; the $options parameter indicates the options during conversion, and the default is 0; the $depth parameter indicates the conversion time The maximum depth, which defaults to 512.
The following is a sample code:
<?php $data = array( 'name' => '张三', 'age' => 20, 'hobby' => array('篮球', '游泳', '音乐'), ); $json = json_encode($data); echo $json; ?>
In the above code, we define an array $data, which contains a subarray named 'hobby'. After using the json_encode function to convert $data into a JSON string, the result is as follows:
{"name":"张三","age":20,"hobby":["篮球","游泳","音乐"]}
2. PHP conversion object is JSON
In addition to converting the array into a JSON format string, we Objects can also be converted to JSON. In PHP, we can use the json_encode function to achieve this function.
The following is a sample code:
<?php class Person { public $name; public $age; public $hobby; function __construct($name, $age, $hobby) { $this->name = $name; $this->age = $age; $this->hobby = $hobby; } } $data = new Person('张三', 20, array('篮球', '游泳', '音乐')); $json = json_encode($data); echo $json; ?>
In the above code, we define a class named Person, which defines three public properties. After creating the $data object, we convert it to a JSON format string using the json_encode function. The result obtained is as follows:
{"name":"张三","age":20,"hobby":["篮球","游泳","音乐"]}
It should be noted that when converting the object to a JSON format string, only the public properties will be converted. If you want to convert private or protected properties into JSON format strings, you can use the __get() method.
3. Convert JSON with Chinese in PHP
When encountering JSON that needs to be converted in Chinese, we need to pay attention to some details. Because the json_encode function converts Chinese into Unicode encoding by default, the Chinese in the JSON format string becomes difficult to read.
You can convert it to Chinese by setting the $options parameter of the json_encode function. The specific method is as follows:
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
Among them, the JSON_UNESCAPED_UNICODE parameter indicates that Unicode-encoded Chinese characters will not be escaped. In this way, Chinese characters in the JSON format string will be displayed normally.
4. PHP Convert JSON to Array or Object
In addition to converting arrays or objects to JSON format strings, we can also convert JSON format strings back to arrays or objects. In PHP, we can use the json_decode function to achieve this function.
The following is a sample code:
In the above code, we assign a JSON format string to the variable $json and use the json_decode function to convert it into an array. Among them, the $data parameter represents the converted array, and the true parameter represents converting the returned object into array format.
Note: If the second parameter is not set to true, a StdClass object will be obtained.
Summary
In PHP, we can easily use the json_encode and json_decode functions to convert arrays, objects and other data types into JSON format strings, and convert JSON format strings back to arrays , object and other data types. This brings great convenience to data exchange and data communication, and also improves our coding efficiency.
The above is the detailed content of How to convert json to string in php. For more information, please follow other related articles on the PHP Chinese website!