[英]How to encode XML to JSON including the root element?
Let's say I have a XML document 假设我有一个XML文档
<?xml version="1.0" encoding="utf-8" ?>
<book>
<title>Book Title</title>
<isbn>123456789</isbn>
</book>
And I want to convert this XML document into a JSON string 我想将此XML文档转换为JSON字符串
<?php
$xml = simplexml_load_file('book.xml');
//print '{"book":';
print json_encode($xml, true);
//print '}';
?>
But this excludes the root element, 'book.' 但这不包括根元素“书”。
Why does json_encode exclude the root element? 为什么json_encode排除根元素?
I think Zend Json Library has an option to include the root. 我认为Zend Json Library可以选择包含根目录。
$jsonContents = Zend_Json::fromXml($xmlStringContents, true);
The true will cause the root to be displayed in the JSON object. 如果为true,则根将显示在JSON对象中。 Not Sure if this can be done with simplexml_load_file aad print json_encode($xml, true);
不确定是否可以通过simplexml_load_file和print json_encode($ xml,true);来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.