gelight/php-json2sml
Composer 安装命令:
composer require gelight/php-json2sml
包简介
PHP Library for conversion JSON to SML
README 文档
README
Converts json into SML document.
What is SML?
Using
Given JSON structure
<?php include_once "vendor/autoload.php"; use GELight\conversion\{JsonToSmlConverter}; $json = <<<JSON { "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 27, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" } ], "children": [ "Aaron" ], "spouse": true } JSON; $converter = new JsonToSmlConverter(); $doc = $converter::convert(json_decode($json)); $root = $doc->getRoot(); // Returns the first value of attribute "lastName" echo $root ->attribute("lastName") ->getValues()[0]; // Returns the first value of attribute "city" in element "address" echo $root ->element("address") ->attribute("city") ->getValues()[0]; // Returns the first value of attribute "number" of second element "phoneNumbers" echo $root ->element("phoneNumbers") ->elements()[1] ->attribute("number") ->getValues()[0]; // Returns the first value of attribute "children" echo $root ->attribute("children") ->getValues()[0];
Result:
Smith New York 646 555-4567 Aaron
Generated SML document structure as string output
# PHP $doc->toString();
sml firstName John lastName Smith isAlive 1 age 27 address streetAddress "21 2nd Street" city "New York" state NY postalCode 10021-3100 end phoneNumbers phoneNumber type home number "212 555-1234" end phoneNumber type office number "646 555-4567" end end children Aaron spouse 1 end
Documentation
JsonToSmlConverter
Creates a new instance of the JSON to SML converter
$converter = new JsonToSmlConverter();
convert
Static method "convert" will convert your JSON in a SML document.
convert(object $jsonObject): SmlDocument
$smlDocument = $converter::convert(json_decode($json));
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-15