macrotux/jsonutils

此包已被 废弃,不再维护。未建议替代包。

JsonUtils,PHP7中编码-解码JSON的实用工具,具有错误控制和异常处理

1.0 2016-05-09 15:10 UTC

This package is not auto-updated.

Last update: 2018-09-29 20:45:53 UTC


README

PHP7中编码-解码JSON的实用工具,具有错误控制和异常处理

Composer

"require": {
 "php": ">=7.0",
  "macrotux/jsonutils": "dev-master"
},

示例

编码数组

try {
  $strJson = \JsonUtils\JsonUtils::encodeArray(["foo"=>"bar"]);
}  catch (\JsonUtils\ExceptionJsonError $e) {
  error_log($e->getMessage());
}

编码对象

try {
  $strJson = \JsonUtils\JsonUtils::encodeObj( new \stdClass() );
}  catch (\JsonUtils\ExceptionJsonError $e) {
  error_log($e->getMessage());
}

解码JSON字符串对象

try {
  $strJson = \JsonUtils\JsonUtils::decodeToObject('{"foo":"bar"}'); // return \stdClass
}  catch (\JsonUtils\ExceptionJsonError $e) {
  error_log($e->getMessage());
}

解码JSON字符串数组

try {
  $strJson = \JsonUtils\JsonUtils::decodeToArray('{"foo":"bar"}'); // return Array
}  catch (\JsonUtils\ExceptionJsonError $e) {
  error_log($e->getMessage());
}