分类 web 下的文章

“web开发杂七杂八”

<?php
...
$json_string = json_encode($data, JSON_PRETTY_PRINT);
...
?>

Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR.

https://www.php.net/manual/en/json.constants.php

SON_PRETTY_PRINT (int)
Use whitespace in returned data to format it.

<?php
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
?>

PHP convert XML to JSON

$xml = simplexml_load_file("states.xml");
echo json_encode($xml);

php json to xml

$xml = new SimpleXMLElement('<root/>');
$this->arrayToXml($array, $xml);
function arrayToXml($array, &$xml){
    foreach ($array as $key => $value) {
        if(is_int($key)){
            $key = "e";
        }
        if(is_array($value)){
            $label = $xml->addChild($key);
            $this->arrayToXml($value, $label);
        }
        else {
            $xml->addChild($key, $value);
        }
    }
}