【问题标题】:Access to an object from an Array JSON [duplicate]从数组 JSON 访问对象 [重复]
【发布时间】:2015-04-24 17:31:28
【问题描述】:

我的问题是我无法访问 JSON 数组中的特定对象。我遵循了很多解决方案,但它们不起作用! 这是我的脚本:

<?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);
    //--------------
    unset($myXml['@attributes']);
    unset($myXml['channel']);

    //$json = json_encode($myXml['item']);
    //var_dump($myXml);    
    $json = json_encode($myXml);
    $arr = json_decode($json, true);
    echo json_encode($arr['item']);

}
}

//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 = 
$myjson = $jsonObj->ParseXMl($url);
//echo ($myjson);

?>

此脚本返回 null。 我只想访问这个数组中的“项目”。

感谢您的帮助。

【问题讨论】:

  • @MarcB:感谢您的指出。

标签: php xml json


【解决方案1】:

改变这一行...

echo json_encode($arr['item']);

...进入这个:

return json_encode($arr['channel']['item']);

然后在文件末尾取消注释您的echo

$myjson = $jsonObj->ParseXMl($url);
echo $myjson;

【讨论】:

  • thxxxxxxxx 它有效。
猜你喜欢
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多