From 289507c0ce1c295b6a5e1e071ae21a9bb95282f3 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 3 May 2016 12:28:15 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0toJson=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=BE=93=E5=87=BAJSON=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index f4f77c13..5962e636 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -169,6 +169,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return !empty($item) ? $item : []; } + /** + * 转换当前模型对象为JSON字符串 + * @access public + * @param integer $options json参数 + * @return string + */ + public function toJson($options = JSON_UNESCAPED_UNICODE) + { + return json_encode($this->toArray(), $options); + } + /** * 获取当前模型对象的主键 * @access public @@ -980,7 +991,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function __toString() { - return json_encode($this->toArray()); + return $this->toJson(); } // JsonSerializable