无法访问 iTunes RSS 源的对象键"@attributes"



我正在尝试解析iTunes RSS提要,方法是通过php将其转换为JSON,然后使用jQuery .ajax插入它。

首先是 php

<?php
     $url = "http://schoolceoshow.libsyn.com/rss";
     $fileContents = file_get_contents($url);
     $fileContents = str_replace(array("n", "r", "t"), '', $fileContents);
     $fileContents = trim(str_replace('"', "'", $fileContents));
     $simpleXml = simplexml_load_string($fileContents);
     $json = json_encode($simpleXml);
     echo $json;
?>

现在是JavaScript

var root = location.origin + "/";
$.ajax({
url:root + "php/podcast.php",
type:"GET",
data:"json",
success:function(data){
    var dataObject = $.parseJSON(data);
    console.log(dataObject.channel.item[0].enclosure);
},
error:function(){
    console.log("failed");
}
});

注销的内容是

Object {@attributes: Object}
  @attributes: Object
    length: "25583209"
    type: "audio/mpeg"
    url: "http://traffic.libsyn.com/schoolceoshow/SchoolCEOShow-007.mp3"

我在这里唯一的问题是访问@attributes密钥。如何访问带有 @ 符号的密钥?谢谢!

解决方案是使用括号表示法访问对象。我添加到我的代码中的内容是

var enclosure = dataObject.channel.item[0].enclosure;
console.log(enclosure["@attributes"]);

答案在以下位置找到:解析带有@的JSON,其中的符号符号(arobase)

最新更新