Javascript PHP解析xml文件中的json

Javascript PHP解析xml文件中的json,javascript,php,arrays,json,parsing,Javascript,Php,Arrays,Json,Parsing,我有点熟悉xml和json解析,但我在这个网站上遇到了一个问题。我试过了 $json_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml"; $json = file_get_contents($json_string); $arr=json_decode($json_string, true); 但它不起作用。当我分析数据时,我发现json中有一些ja

我有点熟悉xml和json解析,但我在这个网站上遇到了一个问题。我试过了

$json_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml"; 
$json = file_get_contents($json_string); 
$arr=json_decode($json_string, true);
但它不起作用。当我分析数据时,我发现json中有一些javascript变量在运行javascript时会变成数据,所以这可能就是它不起作用的原因。不是舒尔,你该如何修复它

我想做的是解析像“9:10CEST”这样的值。。。和“si0_20140930-0710_zm_si.jpg”。。。进入php数组。

使用:

$xml=simplexml_load_file("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml");

不要使用
json\u decode
xml
似乎不是有效的。
它不包含任何数据规则的DTD,甚至不包含标准的DTD。下面的解决方案是有效的。不知道是否有更好的方法:

<?php
    $xml_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml"; 
    $xml = file_get_contents($xml_string);

    // Extract relevant section out of the file
    $start_pos = strpos($xml, "timeline:") + strlen("timeline:");
    $end_pos = strpos($xml, "});");
    $json = substr($xml, $start_pos, $end_pos - $start_pos);

    // Some string replace operations to bind the keys and values within " (double quotes) 
    $json = preg_replace("/(,[a-z]+)/", '"$1', $json);
    $json = preg_replace("/([a-z]+)(:)/", '"$1"$2"', $json);
    $json = str_replace('}', '"}', $json);

    // echo $json; // This string is now in decodable json format
    $arr = json_decode($json, true);
    var_dump($arr);
    return;
?>

您试图解码的是XML而不是JSON。改用
simplexml
。文件是xml。那么,为什么要使用json_decode()?不适用于xml格式该文件上的数据格式似乎很奇怪。它既不是XML也不是JSON。难怪语言设施不起作用。我想你必须编写一些自定义逻辑。我尝试了$url=file\u get\u contents(“);$xml=simplexml\u load\u string($url);print\r($xml);但它不起作用。它给了我SimpleXMLElement对象(),这是我一直在寻找的对象…我对它进行了一些修改,现在已经很完美了!THX。
$data = file_get_contents("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml");
$data = str_replace(array('<?xml version="1.0" encoding="utf-8"?><pujs><![CDATA[',']]></pujs>'),"",$data);
echo '<script type="text/javascript">'.$data."</script>";
<?php
    $xml_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml"; 
    $xml = file_get_contents($xml_string);

    // Extract relevant section out of the file
    $start_pos = strpos($xml, "timeline:") + strlen("timeline:");
    $end_pos = strpos($xml, "});");
    $json = substr($xml, $start_pos, $end_pos - $start_pos);

    // Some string replace operations to bind the keys and values within " (double quotes) 
    $json = preg_replace("/(,[a-z]+)/", '"$1', $json);
    $json = preg_replace("/([a-z]+)(:)/", '"$1"$2"', $json);
    $json = str_replace('}', '"}', $json);

    // echo $json; // This string is now in decodable json format
    $arr = json_decode($json, true);
    var_dump($arr);
    return;
?>