aalfiann/json-class-php

一个用于更优处理 JSON 的类。

1.3.1 2019-04-08 10:28 UTC

This package is auto-updated.

Last update: 2024-09-08 23:04:25 UTC


README

Version Total Downloads License

一个用于更优处理 JSON 的类。

安装

通过 Composer 安装此包。

composer require "aalfiann/json-class-php:^1.0"

用法

编码

require_once ('vendor/autoload.php');
use \aalfiann\JSON;

$data = [
    'result'=>'just make a test!',
    'data' => [
        'id' => '1',
        'user' => 'your name',
        'email' => 'your_email@gmail.com',
        'website' => 'http://yourdomain.com',
        'non-utf8' => 'السلام علیکم ورحمة الله وبرکاته!'
    ]
];

header('Content-Type: application/json');
$json = new JSON;
echo $json->encode($data,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);

解码

require_once ('vendor/autoload.php');
use \aalfiann\JSON;

$data = '{"result":"just make a test!","data":{"id":"1","user":"your name","email":"your_email@gmail.com","website":"http://yourdomain.com","non-utf8":"\u00d8\u00a7\u00d9\u0084\u00d8\u00b3\u00d9\u0084\u00d8\u00a7\u00d9\u0085 \u00d8\u00b9\u00d9\u0084\u00db\u008c\u00da\u00a9\u00d9\u0085 \u00d9\u0088\u00d8\u00b1\u00d8\u00ad\u00d9\u0085\u00d8\u00a9 \u00d8\u00a7\u00d9\u0084\u00d9\u0084\u00d9\u0087 \u00d9\u0088\u00d8\u00a8\u00d8\u00b1\u00da\u00a9\u00d8\u00a7\u00d8\u00aa\u00d9\u0087!"},"logger":{"timestamp":"2018-09-17 13:53:12","uniqid":"5b9f95a812c0f"}}';

$json = new JSON;
echo var_dump($json->decode($data,true));

调试

您可以通过将属性 debug 设置为 true 来轻松调试。

require_once ('vendor/autoload.php');
use \aalfiann\JSON;

header('Content-Type: application/json');
$json = new JSON;
$json->debug=true;
echo $json->encode($data,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);

链式用法示例

require_once ('vendor/autoload.php');
use \aalfiann\JSON;

header('Content-Type: application/json');
$json = new JSON;
echo $json->withSanitizer()->withLog()->encode(['user'=>'yourname'],JSON_PRETTY_PRINT);

属性

  • $withlog=false,$sanitize=false,$ansii=false,$debug=false,$makesimple=false,$trim=false;

链式函数

  • withSanitizer($sanitize=true) 这将在执行 json_encode 之前对您的数据数组进行清理。
  • withTrim($trim=true) 这将在执行 json_encode 或 json_decode 之前对数据数组或字符串进行修剪。
  • withLog($withlog=true) 这将日志数据附加到您的 JSON 中。
  • setAnsii($ansii=true) 这将使清理器能够处理 ANSII 字符。
  • setDebug($debug=true) 这用于调试目的。
  • makeSimple($makesimple=true) 这将隐藏调试输出中的附加数据。

主函数

  • encode($data,$options=0,$depth=512) 将数组或字符串编码为 JSON 格式。
  • decode($json,$assoc=false,$depth=512,$options=0) 将 JSON 字符串解码为 stdClass/array
  • isValid($json=null) 用于判断 JSON 字符串是否有效。

辅助函数

  • convertToUTF8($string) 将字符串转换为有效的 UTF8 字符。
  • convertToUTF8Ansii($string) 将字符串转换为有效的 UTF8 字符(支持 ANSII 字符)。
  • trimValue($string) 修剪数据数组或字符串,以删除空白或任何被视为空值的字符。
  • debug_encode($string,$options=0,$depth=512) 用于测试 json 编码的调试器。
  • debug_decode($json,$assoc=false,$depth=512,$options=0) 用于测试 json 解码的调试器。
  • errorMessage($jsonlasterror,$content) 关于 JSON 的错误信息。
  • fixControlChar($string) 最常见的固定隐藏控制字符,这些字符会导致 JSON 解码失败。
  • modifyJsonStringInArray($data,$jsonfield,$setnewfield="") 将某些字段数组中的 JSON 数据字符串修改为更漂亮的 JSON 数据结构。
  • concatenate($data,$escape=true,$options=0,$depth=512) 连接 JSON 数据
  • minify($json) 最小化 JSON 字符串