estaheri/json

在PHP中使用JSON编码字符串的简单方法

1.1 2024-08-24 17:25 UTC

This package is auto-updated.

Last update: 2024-09-24 17:42:54 UTC


README

在PHP中使用JSON编码字符串的简单方法。

安装

  1. Composer : composer require estaheri/json
  2. 手动:从GitHub的发布页面下载,然后在您的代码中引入类文件 json.php

示例

<?php
// validate is a string json
$is_json = json::_is($string);
// json decode or encode string
$json_decode = json::_in($json_string);
$json_encode = json::_out($data);
// get or update a key/keys from a json string
$json_string = json::update($json_string,['firstName'=>'Will','lastName'=>'Smith']);
$first_name = json::get($json_string,'firstName');
$name = json::get($json_string,['firstName','lastName']);
// remove a key/keys from a json string
$json_string = json::remove($json_string,'firstName');
$json_string = json::remove($json_string,['lastName','age']);
// convert array or object or json string to these three types
$array = json::to_array($data);
$json = json::to_json($data);
$object = json::to_object($data);

问题

为什么我们应该使用这个类,而PHP本身就有JSON方法呢?

我只是为了使代码更简洁、更短小而编写了这个类。
例如

<?php
$json_string='{"id": "1","firstName": "Tom","lastName": "Cruise"}';
####################################################
// using php itself
// update a key from json string
$decoded=json_decode($json_string);
$decoded->firstName='Will';
$decoded->lastName='Smith';
$json_string = json_encode($decoded);
####################################################
// using this library
// update a key from json string
$json_string = json::update($json_string,['firstName'=>'Will','lastName'=>'Smith']);

顺便说一下,我是为了自己的使用而编写的,但我认为也许有人需要它!

文档在哪里?

所有方法都有phpDoc文档,因此目前不需要编写文档。

  • 此外,它只是一个具有简单方法的类!