dcarbone/json-writer-plus

适用于 PHP 5.3+ 的简单 JSON 写入库

0.3.0 2018-01-30 02:18 UTC

This package is auto-updated.

Last update: 2024-09-17 09:34:06 UTC


README

适用于 PHP 5.3+ 的简单 JSON 写入库

此库的目标是提供一种方式来构建一个类似于 PHP XMLWriter 类的 JSON 对象。

在您的 Composer 应用中包含

添加

"dcarbone/json-writer-plus" : "0.2.*"

到您的应用的 composer.json 文件中。

在此了解有关 Composer 的更多信息: https://getcomposer.org.cn/

基本用法

开始创建您自己的 JSON 对象

use \DCarbone\JsonWriterPlus;

// Create instance
$jsonWriter = new JsonWriterPlus();

// Start the writer object
$jsonWriter->startJson();

// Open a new object for population
$jsonWriter->writeStartObject();

// Directly write a property name and value to the opened object
$jsonWriter->writeObjectProperty('PropertyKey', 'PropertyValue');

// Write a new property name for later population
$jsonWriter->writeObjectPropertyName('ValueArray');

// Start an array.  Since we wrote a new property name above, it is automatically appended
// to the parent object at the previously specified key
$jsonWriter->writeStartArray();

// Add two values to the array
$jsonWriter->writeValue("Value1");
$jsonWriter->writeValue("Value2");

// Close the array
$jsonWriter->writeEndArray();

// Close the parent object
$jsonWriter->writeEndObject();

// Close the writer
$jsonWriter->endJson();

// See the "jsonized" version of the above actions
echo $jsonWriter->getEncoded()."\n";

// See the internal representation of the above actions as PHP sees it
echo '<pre>';
var_dump($jsonWriter->getUnencoded());
echo '</pre>';

上面的代码块将产生以下输出

{"PropertyKey":"PropertyValue","ValueArray":["Value1","Value2"]}

object(stdClass)#4 (2) {
  ["PropertyKey"]=>
  string(13) "PropertyValue"
  ["ValueArray"]=>
  array(2) {
    [0]=>
    string(6) "Value1"
    [1]=>
    string(6) "Value2"
  }
}

开始

上面的示例演示了一个具有 Object 作为主要元素的 Json 输出,但您也可以从数组开始

// Initialize writer
$jsonWriter = new JsonWriterPlus();

// Start writer
$jsonWriter->startJson();

// Open root array
$jsonWriter->writeStartArray();

// Open object as first item of root array
$jsonWriter->writeStartObject();
$jsonWriter->writeObjectProperty('Property1', 'This object is inside an array!');
$jsonWriter->writeEndObject();

// Open new array as 2nd item of root array
$jsonWriter->writeStartArray();
$jsonWriter->writeValue('Nested array value 1');
$jsonWriter->writeValue('Nested array value 2');
$jsonWriter->writeEndArray();

// Write a string value directly to root array as 3rd item
$jsonWriter->writeValue('Root array value');

$jsonWriter->writeEndArray();

$jsonWriter->endJson();

echo $jsonWriter->getEncoded()."\n";
echo '<pre>';
var_dump($jsonWriter->getUnencoded());
echo '</pre>';

上面的输出将显示

[{"Property1":"This object is inside an array!"},["Nested array value 1","Nested array value 2"],"Root array value"]

array(3) {
  [0]=>
  object(stdClass)#4 (1) {
    ["Property1"]=>
    string(31) "This object is inside an array!"
  }
  [1]=>
  array(2) {
    [0]=>
    string(20) "Nested array value 1"
    [1]=>
    string(20) "Nested array value 2"
  }
  [2]=>
  string(16) "Root array value"
}

有趣的事情

假设您已经打开了一个 JsonWriter 实例并且已经构建了一个数组,并且您希望直接将整个内容附加到 Json 输出中,而不需要循环和手动执行操作。当然可以!

$array = array(
    'Look at all my cool information',
    'My information is the coolest'
);

$jsonWriter = new JsonWriterPlus();

$jsonWriter->startJson();

$jsonWriter->appendArray($array);

$jsonWriter->endJson();

echo $jsonWriter->getEncoded()."\n";
echo '<pre>';
var_dump($jsonWriter->getUnencoded());
echo '</pre>';

上面的输出将显示

["Look at all my cool information","My information is the coolest"]
array(2) {
  [0]=>
  string(31) "Look at all my cool information"
  [1]=>
  string(29) "My information is the coolest"
}
$array = array(
    'Look at all my cool information',
    'property1' => 'property 1 is the coolest property',
    'property2' => 'property2 is the next coolest property',
    'this is also cool information'
);

$jsonWriter = new JsonWriterPlus();

$jsonWriter->startJson();

$jsonWriter->appendArray($array);

$jsonWriter->endJson();

echo $jsonWriter->getEncoded()."\n";
echo '<pre>';
var_dump($jsonWriter->getUnencoded());
echo '</pre>';

将产生

["Look at all my cool information",{"property1":"property 1 is the coolest property"},{"property2":"property2 is the next coolest property"},"this is also cool information"]
array(4) {
  [0]=>
  string(31) "Look at all my cool information"
  [1]=>
  object(stdClass)#4 (1) {
    ["property1"]=>
    string(34) "property 1 is the coolest property"
  }
  [2]=>
  object(stdClass)#5 (1) {
    ["property2"]=>
    string(38) "property2 is the next coolest property"
  }
  [3]=>
  string(29) "this is also cool information"
}

您也可以通过 appendObject($object) 方法对对象执行类似操作。