当前位置:网站首页>JSON instance (I)
JSON instance (I)
2022-06-26 01:27:00 【Life needs depth】
PHP JSON
In this chapter, we will show you how to use PHP Language to encode and decode JSON object .
Environment configuration
stay php5.2.0 And above versions have been built in JSON Expand .
JSON function
| function | describe |
|---|---|
| json_encode | On variables JSON code |
| json_decode | Yes JSON Format of the string , Convert to PHP Variable |
| json_last_error | Returns the last error that occurred |
json_encode
PHP json_encode() Used to modify variables JSON code , If the function is executed successfully, it returns JSON data , Otherwise return to FALSE .
grammar
string json_encode ( $value [, $options = 0 ] )
Parameters
- value: The value to encode . This function only applies to UTF-8 The encoded data is valid .
options: Binary mask consisting of the following constants JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
It should be noted that JSON_UNESCAPED_UNICODE Options , If we don't want Chinese to be encoded , You can add this option .
example
The following example demonstrates how to PHP Array to JSON Format data :
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
The execution result of the above code is :
{"a":1,"b":2,"c":3,"d":4,"e":5}
The following example demonstrates how to PHP Object to JSON Format data :
example
<?php
class Emp {
public $name = "";
public $hobbies = "";
public $birthdate = "";
}
$e = new Emp();
$e->name = "sachin";
$e->hobbies = "sports";
$e->birthdate = date('m/d/Y h:i:s a', "8/5/1974 12:20:03 p");
$e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 12:20:03"));
echo json_encode($e);
?>
The execution result of the above code is :
{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}
Use JSON_UNESCAPED_UNICODE Options
<?php
$arr = array('runoob' => ' Novice tutorial ', 'taobao' => ' TaoBao ');
echo json_encode($arr); // Coded Chinese
echo PHP_EOL; // A newline
echo json_encode($arr, JSON_UNESCAPED_UNICODE); // No coding Chinese
?>
The execution result of the above code is :
{"runoob":"\u83dc\u9e1f\u6559\u7a0b","taobao":"\u6dd8\u5b9d\u7f51"}
{"runoob":" Novice tutorial ","taobao":" TaoBao "}
json_decode
PHP json_decode() Function is used for JSON Format of the string , And converted to PHP Variable .
grammar
mixed json_decode ($json_string [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
Parameters
json_string: To be decoded JSON character string , Must be UTF-8 Encoding data
assoc: When the parameter is TRUE when , Will return an array ,FALSE Returns the object .
depth: Parameter of integer type , It specifies the recursion depth
options: Binary mask , Currently only supported JSON_BIGINT_AS_STRING .
example
The following example demonstrates how to decode JSON data :
example
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
The execution result of the above code is :
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}边栏推荐
- 如何有效地推广产品
- RT-Thread 项目工程搭建和配置--(Env Kconfig)
- Black box test - decision table method of test cases
- Web信息收集,互联网上的裸奔者
- Endnote IEEE TRANSACTIONS ON INDUSTRIAL ELECTRONICS/TIE/TPEL 参考文献格式模板
- idea配置
- STM32 key development foundation
- Camera - 02 image sensor
- Dgus new upgrade: fully support digital video playback function
- 2022防爆电气操作证考试题库及模拟考试
猜你喜欢

Oracle数据库开启备份准备工作

Duck feeding data instant collection solution resources

生信周刊第33期

Multiple interface calls, using promise all、Promise. Race and promise any

数组中的第K个最大元素

远程增量同步神器rsync

Musk vs. jobs, who is the greatest entrepreneur in the 21st century

Shengxin weekly issue 34

Development and monitoring of fusion experiment pulse power supply by LabVIEW

The overall process of adding, deleting, modifying and querying function items realized by super detailed SSM framework
随机推荐
FreeRTOS+STM32L+ESP8266+MQTT协议传输温湿度数据到腾讯云物联网平台
C disk cleaning strategy of win10 system
Technical foreword - metauniverse
Nacos registry
idea配置
JS reverse case: cracking login password
毕业季你考虑好去留了吗
Casually painted
生信周刊第33期
C thread pool control semaphore
FIFO code implemented in C language
通过电脑获取WIFI密码(只能连接过的WiFi)
Laravel基础课 路由和MVC——路由
Endnote IEEE TRANSACTIONS ON INDUSTRIAL ELECTRONICS/TIE/TPEL 参考文献格式模板
新库上线 | CnOpenData中国新房信息数据
Discrete Mathematics - 01 mathematical logic
. Net using access 2010 database
MySQL例题一 综合案例(多条件组合查询)
[flower carving experience] 11 start esp32c3
Handling of @charset UTF-8 warning problems during vite packaging and construction;