在PHP 中使用JSON

在 PHP 中使用 JSON

本教程将会教我们如何使用 PHP 编程语言编码和解码 JSON 对象。让我们先来准备环境以便针对 JSON 进行 PHP 编程。

环境

从 PHP 5.2.0 开始默认捆绑了 JSON 扩展并被编译到 PHP 中。

JSON 函数

函数程序库
json_encode返回一个值的 JSON 表示形式。
json_decode解码为一个 JSON 字符串。
json_last_error返回最后一次出现的错误。

使用 PHP 编码 JSON(json_encode)

PHP 的 json_encode() 函数用于在 PHP 中编码 JSON。编码成功时这个函数返回给定值的 JSON 表示形式,失败则返回 FALSE。

语法:

string json_encode ( $value [, $options = 0 ] )

参数:

  • value: 要编码的值。这个函数只能用于 UTF-8 编码的数据。

示例:

下面的例子展示了如何使用 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', strtotime("8/5/1974 12:20:03") echo json_encode($e ?>

执行时会生成如下所示结果:

{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}

使用 PHP 解码 JSON(json_decode)

PHP 的 json-decode() 函数用于在 PHP 中解码 JSON。这个函数返回从 JSON 解码到适当 PHP 类型的值。

语法:

mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

参数:

  • __json_string:__ 编码的字符串,必须是 UTF-8 编码的数据。

示例:

下面例子展示了如何使用 PHP 解码 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) }