用php删除json变量中的一些信息
我的问题是,我正在解析一个XML文件,该文件包含一些我不想导出为JSON数据的信息。在我的例子中,我需要一个从第一个“[”caratere返回json数据的函数 这是php代码:用php删除json变量中的一些信息,php,json,Php,Json,我的问题是,我正在解析一个XML文件,该文件包含一些我不想导出为JSON数据的信息。在我的例子中,我需要一个从第一个“[”caratere返回json数据的函数 这是php代码: <?php class XmlToJsonConverter { public function ParseXML ($url) { $fileContents= file_get_contents($url); // Remove tabs, newline,
<?php
class XmlToJsonConverter {
public function ParseXML ($url) {
$fileContents= file_get_contents($url);
// Remove tabs, newline, whitespaces in the content array
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$myXml = simplexml_load_string($fileContents);
$json = json_encode($myXml);
return $json;
}
}
//Path of the XML file
$url= 'http://www.lequipe.fr/rss/actu_rss_Football.xml';
//Create object of the class
$jsonObj = new XmlToJsonConverter();
//Pass the xml document to the class function
$myjson = $jsonObj->ParseXMl($url);
print_r ($myjson);
?>
ParseXMl($url);
打印($myjson);
?>
这是JSON结果的一部分:
{@attributes:{“版本”:“2.0”},频道:{“标题”:“L'Equipe.fr Actu Football”,“链接”:http://www.lequipe.fr“,”描述“:”L'Equipe.fr,Toute L'Implementat\u00e9 du football“,”语言“:”fr“,”版权“:”版权“,”L'Equipe.fr“,”发布日期“:”Wed,Apr 2015 16:31:08+0200”,“图片“:{”url:”http://www.lequipe.fr/rss/logo_RSS.gif“,“标题”:“设备fr”,“链接”:http://www.lequipe.fr“,”宽度“:”119“,”高度“:”28“}”,项目“:[{”标题“:”脚-茶”
我希望结果从“[”开始
谢谢在编码json之前删除所有不需要的属性:
public function ParseXML ($url) {
$fileContents= file_get_contents($url);
// Remove tabs, newline, whitespaces in the content array
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$myXml = simplexml_load_string($fileContents);
//--------------
unset($myXml['@attributes']);
unset($myXml['channel']);
unset($myXml['image']);
//--------------
$json = json_encode($myXml);
return $json;
}
或者,如果您只需要该项目:
public function ParseXML ($url) {
$fileContents= file_get_contents($url);
// Remove tabs, newline, whitespaces in the content array
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$myXml = simplexml_load_string($fileContents);
//--------------
$json = json_encode($myXml['item']);
return $json;
}
使用xPath从xml/htmli中选择特定节点。我正在为android应用程序生成此json。我必须从字符“[”开始,以便android可以将其作为JsonArrayObject读取。我只想删除第一个“[”字符之前的所有文本。我将通过正则表达式将此字符串置于“:”的正后方,并从中选择其余部分[抱歉,我不明白你的意思!!这是一个JSON字符串。Javascript可以匹配正则表达式并选择你描述的模式。Google for regexit不起作用!!它总是返回所有数据!!我尝试了两种解决方案!抱歉,回复太晚了,我离开了一段时间,忘了回来。你还有问题吗?我不确定是什么原因y您仍然看到了其他属性,但我相信这两种解决方案都很好。您是否尝试过跟踪php脚本?(例如print_r($myXml),print_r($json),然后退出;强制退出)