Book/json-Phpdoc专题

JavaScript对象符号(JSON)

目录

简介

Exception thrown if JSON_THROW_ON_ERROR option is set for <span class="function">json_encode or <span class="function">json_decode. code contains the error type, for possible values see json_last_error.

类摘要

JsonException

class JsonException <span class="ooclass"> extends Exception {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}

简介

实现 JsonSerializable 的类可以 在 json_encode 时定制他们的 JSON 表示法。

接口摘要

JsonSerializable

class JsonSerializable {

/* 方法 */

abstract <span class="modifier">public mixed <span class="methodname">jsonSerialize ( <span class="methodparam">void )

}

JsonSerializable::jsonSerialize

指定需要被序列化成 JSON 的数据

说明

abstract <span class="modifier">public mixed <span class="methodname">JsonSerializable::jsonSerialize ( <span class="methodparam">void )

序列化物体(Object)成能被 json_encode 原生地序列化的值。

参数

此函数没有参数。

返回值

返回能被 json_encode 序列化的数据, 这个值可以是除了 resource 外的任意类型。

范例

示例 #1 <span class="methodname">JsonSerializable::jsonSerialize 例子 returning an array

<?php
class ArrayValue implements JsonSerializable {
    public function __construct(array $array) {
        $this->array = $array;
    }

    public function jsonSerialize() {
        return $this->array;
    }
}

$array = [1, 2, 3];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>

以上例程会输出:

[
    1,
    2,
    3
]

示例 #2 <span class="methodname">JsonSerializable::jsonSerialize 例子 返回了一个关联数组 array

<?php
class ArrayValue implements JsonSerializable {
    public function __construct(array $array) {
        $this->array = $array;
    }

    public function jsonSerialize() {
        return $this->array;
    }
}

$array = ['foo' => 'bar', 'quux' => 'baz'];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>

以上例程会输出:

{
    "foo": "bar",
    "quux": "baz"
}

示例 #3 <span class="methodname">JsonSerializable::jsonSerialize 例子 返回一个 integer

<?php
class IntegerValue implements JsonSerializable {
    public function __construct($number) {
        $this->number = (integer) $number;
    }

    public function jsonSerialize() {
        return $this->number;
    }
}

echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);
?>

以上例程会输出:

1

示例 #4 <span class="methodname">JsonSerializable::jsonSerialize 例子 返回一个 string

<?php
class StringValue implements JsonSerializable {
    public function __construct($string) {
        $this->string = (string) $string;
    }

    public function jsonSerialize() {
        return $this->string;
    }
}

echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT);
?>

以上例程会输出:

"Hello!"

本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除