温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP中的JSON与XML格式怎么转换

发布时间:2023-04-03 10:18:51 来源:亿速云 阅读:110 作者:iii 栏目:编程语言

今天小编给大家分享一下PHP中的JSON与XML格式怎么转换的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

一、JSON转XML

PHP提供了可用于将JSON数据转换成XML格式的函数json_decode()。其语法如下:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

其中,$json表示要转换的JSON字符串,$assoc表示是否将JSON对象转换成关联数组(默认为false),$depth表示最大递归深度(默认为512),$options表示转换选项(默认为0)。

下面是一个将JSON数组转换成XML的例子:

<?php
// JSON数据
$json_data = '{
    "students": [
        {
            "name": "David",
            "age": 20,
            "score": {
                "English": 90,
                "Math": 85,
                "Chinese": 95
            }
        },
        {
            "name": "Tom",
            "age": 22,
            "score": {
                "English": 80,
                "Math": 75,
                "Chinese": 85
            }
        }
    ]
}';

// 将JSON数据转换成PHP数组
$php_data = json_decode($json_data, true);

// 将PHP数组转换成XML格式
$xml_data = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><data></data>');
array_to_xml($php_data, $xml_data);

// 输出XML格式数据
header('Content-type: text/xml');
echo $xml_data->asXML();

// 将数组转换成XML格式的函数
function array_to_xml($arr, &$xml) {
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            if (!is_numeric($key)) {
                $subnode = $xml->addChild("$key");
                array_to_xml($value, $subnode);
            } else {
                array_to_xml($value, $xml);
            }
        } else {
            $xml->addChild("$key", htmlspecialchars("$value"));
        }
    }
}
?>

上述代码首先将JSON字符串转换成PHP数组,然后再使用递归函数将PHP数组转换成XML格式。

输出XML格式数据如下:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <students>
    <0>
      <name>David</name>
      <age>20</age>
      <score>
        <English>90</English>
        <Math>85</Math>
        <Chinese>95</Chinese>
      </score>
    </0>
    <1>
      <name>Tom</name>
      <age>22</age>
      <score>
        <English>80</English>
        <Math>75</Math>
        <Chinese>85</Chinese>
      </score>
    </1>
  </students>
</data>

二、XML转JSON

要将XML格式转换成JSON格式,则需要先将XML转换成PHP数组,再使用json_encode()函数将PHP数组转换成JSON字符串。下面是一个将XML转换成JSON的例子:

<?php
// XML数据
$xml_data = '<?xml version="1.0" encoding="UTF-8"?>
<data>
  <students>
    <0>
      <name>David</name>
      <age>20</age>
      <score>
        <English>90</English>
        <Math>85</Math>
        <Chinese>95</Chinese>
      </score>
    </0>
    <1>
      <name>Tom</name>
      <age>22</age>
      <score>
        <English>80</English>
        <Math>75</Math>
        <Chinese>85</Chinese>
      </score>
    </1>
  </students>
</data>';

// 将XML数据转换成PHP数组
$php_data = xml_to_array(simplexml_load_string($xml_data));

// 将PHP数组转换成JSON字符串
$json_data = json_encode($php_data);

// 输出JSON格式数据
header('Content-type: text/json');
echo $json_data;

// 将XML数据转换成PHP数组的函数
function xml_to_array($xml) {
    $arr = array();
    foreach ($xml->children() as $element) {
        if (count($element->children()) == 0) {
            $arr[$element->getName()] = strval($element);
        } else {
            $arr[$element->getName()][] = xml_to_array($element);
        }
    }
    return $arr;
}
?>

上述代码首先将XML字符串通过simplexml_load_string()函数转换成SimpleXMLElement对象,再通过递归函数将SimpleXMLElement对象转换成PHP数组。最后使用json_encode()函数将PHP数组转换成JSON字符串。

输出JSON格式数据如下:

{
  "students": [
    {
      "name": "David",
      "age": "20",
      "score": {
        "English": "90",
        "Math": "85",
        "Chinese": "95"
      }
    },
    {
      "name": "Tom",
      "age": "22",
      "score": {
        "English": "80",
        "Math": "75",
        "Chinese": "85"
      }
    }
  ]
}

以上就是“PHP中的JSON与XML格式怎么转换”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI