Php 将xml转换为json时出错
使用Php 将xml转换为json时出错,php,xml,json,parsing,Php,Xml,Json,Parsing,使用json\u encode()函数时,出现未定义函数的错误。我需要将xml数据转换为json数据。 我使用的代码是: $xml = simplexml_load_string($retValue); echo json_encode($xml); 我也试过: $xml = simplexml_load_string($retValue); $array = objectsIntoArray($xml); $array = json_encode($array); print_r($arra
json\u encode()
函数时,出现未定义函数的错误。我需要将xml数据转换为json数据。
我使用的代码是:
$xml = simplexml_load_string($retValue);
echo json_encode($xml);
我也试过:
$xml = simplexml_load_string($retValue);
$array = objectsIntoArray($xml);
$array = json_encode($array);
print_r($array);
die;
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices);
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
但是在这个objectsIntoArray()
和json\u encode()
中,都抛出了函数未定义的错误。
$retValue
包含xml数据。我不知道为什么会出现这个错误。
我错过了什么吗。我对php完全陌生,对它不太了解。
请帮我解决这个问题。看起来PHP没有启用JSON扩展。从PHP5.2.0开始,默认情况下,JSON扩展是绑定和启用的。但是,您仍然可以使用JSON构建PHP 您应该通过创建名为
phpinfo.php
的脚本来检查是否存在这种情况,该脚本的代码如下:
<?php
phpinfo();
如果您使用的是PHP5.1.x。否则,请告知您正在使用的操作系统(和发行版)以及如何安装PHP
如果您自己没有安装PHP,请联系您的系统管理员/托管提供商
或者您也可以使用它。看起来您使用的是较旧版本的PHP4.3.1。或者您的
simplexml\u load\u字符串
被禁用。
核对
echo phpinfo() 发布完整的错误消息。您使用的是哪个PHP版本?我得到的完整错误是:“调用未定义的函数objectsIntoArray()”。对于json_encode()@sneha,同样的错误消息如果代码与上面的代码完全相同,则不会出现该错误。所以你在做一些不同的事情。@Arnold…我刚刚发布了我正在使用的相同代码。我需要为此包含任何其他库吗?我遗漏了什么吗?@Arnold….是不是我在objectsIntoArray()函数调用中得到了相同的错误?好的,我可以使用其他的吗?我需要在数据表中显示xml数据。还有其他建议吗?@sneha在答案中添加了一个lib。你查过phpinfo吗?@Arnold…是的,我查过phpinfo。版本为5.2.6请发布完整的错误消息。同时检查以下链接:
json support enabled
json version 1.2.1