当前位置:网站首页>JSON实例(一)
JSON实例(一)
2022-06-25 23:49:00 【生活需要深度】
PHP JSON
本章节我们将为大家介绍如何使用 PHP 语言来编码和解码 JSON 对象。
环境配置
在 php5.2.0 及以上版本已经内置 JSON 扩展。
JSON 函数
| 函数 | 描述 |
|---|---|
| json_encode | 对变量进行 JSON 编码 |
| json_decode | 对 JSON 格式的字符串进行解码,转换为 PHP 变量 |
| json_last_error | 返回最后发生的错误 |
json_encode
PHP json_encode() 用于对变量进行 JSON 编码,该函数如果执行成功返回 JSON 数据,否则返回 FALSE 。
语法
string json_encode ( $value [, $options = 0 ] )
参数
- value: 要编码的值。该函数只对 UTF-8 编码的数据有效。
options:由以下常量组成的二进制掩码 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。
要注意的是 JSON_UNESCAPED_UNICODE 选项,如果我们不希望中文被编码,可以添加该选项。
实例
以下实例演示了如何将 PHP 数组转换为 JSON 格式数据:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
以上代码执行结果为:
{"a":1,"b":2,"c":3,"d":4,"e":5}
以下实例演示了如何将 PHP 对象转换为 JSON 格式数据:
实例
<?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);
?>
以上代码执行结果为:
{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}
使用 JSON_UNESCAPED_UNICODE 选项
<?php
$arr = array('runoob' => '菜鸟教程', 'taobao' => '淘宝网');
echo json_encode($arr); // 编码中文
echo PHP_EOL; // 换行符
echo json_encode($arr, JSON_UNESCAPED_UNICODE); // 不编码中文
?>
以上代码执行结果为:
{"runoob":"\u83dc\u9e1f\u6559\u7a0b","taobao":"\u6dd8\u5b9d\u7f51"}
{"runoob":"菜鸟教程","taobao":"淘宝网"}
json_decode
PHP json_decode() 函数用于对 JSON 格式的字符串进行解码,并转换为 PHP 变量。
语法
mixed json_decode ($json_string [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
参数
json_string: 待解码的 JSON 字符串,必须是 UTF-8 编码数据
assoc: 当该参数为 TRUE 时,将返回数组,FALSE 时返回对象。
depth: 整数类型的参数,它指定递归深度
options: 二进制掩码,目前只支持 JSON_BIGINT_AS_STRING 。
实例
以下实例演示了如何解码 JSON 数据:
实例
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
以上代码执行结果为:
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)
}边栏推荐
- 100ask seven day IOT training camp learning notes - bare metal program framework design
- From query database performance optimization to redis cache - talk about cache penetration, avalanche and breakdown
- 开窍之问答
- Freertos+stm32l+esp8266+mqtt protocol transmits temperature and humidity data to Tencent cloud IOT platform
- C#使用MySql进行操作
- 关于HC-12无线射频模块使用
- 超详细SSM框架实现增删改查功能项目整体流程
- Daily question: the difference between threads and processes
- Summary of push-pull output and open drain output of STM32 and failure of analog IIC driving mlx90615
- Enlightenment Q & A
猜你喜欢

Music spectrum display toy -- implementation and application of FFT in stm32

返回值为Object型方法调用equals()
![[flower carving experience] 11 start esp32c3](/img/ff/d7c266893ef4ef70c6536085927e0e.jpg)
[flower carving experience] 11 start esp32c3

Idea configuration

多接口调用,使用Promise.all、Promise.race和Promise.any

STM32GPIO

使用Gin框架运行Demo时报错“ listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden”

Web信息收集,互联网上的裸奔者

黑盒测试 — 测试用例 之 判定表法看这一篇就够了

Technical foreword - metauniverse
随机推荐
[Excel知识技能] Excel数据类型
Tools - API document generation tool
模板引擎——FreeMarker初体验
制作3D浪漫炫酷相册【附源码】
leetcode 300. Longest Increasing Subsequence 最长递增子序列 (中等)
Idempotence of interfaces -- talk about idempotence of interfaces in detail, that is, solutions
I2C protocol
[learn FPGA programming from scratch -44]: vision chapter - integrated circuit helps high-quality development in the digital era -1- main forms of integrated circuit chips
ETCD数据库源码分析——集群间网络层服务端接口
STM32 key development foundation
Modelsim simulation FFT core cannot be simulated solution (qsys)
数据分析——切片器、数据透视表与数据透视图(职场必备)
Discrete Mathematics - 01 mathematical logic
Handling of @charset UTF-8 warning problems during vite packaging and construction;
下载安装Flume
Development and monitoring of fusion experiment pulse power supply by LabVIEW
SPI protocol
STM32GPIO
shell正则表达式
安卓缓存使用工具类