http://php.net/manual/en/function.json-encode.php4
Ray.Paseur often uses Gmail ¶7 months ago
If you're wondering whether a JSON string can be an analog of an XML
document, the answer is probably "nope." XML supports attributes, but
JSON does not. A JSON string generated by json_encode(), when called on
a SimpleXML object, will not have the attributes and no error or
exception will issue - the original data will simply be lost. To see
this in action:
<?php
error_reporting(E_ALL);
echo '<pre>';
// STARTING FROM XML
$xml = <<<EOD
<?xml version="1.0" ?>
<ingredients>
<ingredient>
<name>tomatoes</name>
<quantity type="cup">4</quantity>
</ingredient>
<ingredient>
<name>salt</name>
<quantity type="tablespoon">2</quantity>
</ingredient>
</ingredients>
EOD;
// CREATES AN ARRAY OF SimpleXMLElement OBJECTS
$obj = SimpleXML_Load_String($xml);
var_dump($obj);
echo PHP_EOL;
// SHOW THE ATTRIBUTES HIDDEN IN THE SimpleXMLElement OBJECTS
foreach ($obj as $sub)
{
echo PHP_EOL . (string)$sub->quantity . ' ' . (string)$sub->quantity['type'];
}
echo PHP_EOL;
// USING THE OBJECT, CREATE A JSON STRING
$jso = json_encode($obj);
echo htmlentities($jso); // 'type' IS LOST
echo PHP_EOL;
PHP官网上的一个user contributed note 也提到了type属性丢失