对于itunes RSS feed,无法访问Object键“@attributes”

时间:2014-01-16 16:55:38

标签: javascript php json parsing rss

我尝试通过php将其转换为JSON然后使用jQuery .ajax插入来解析itunes RSS提要。

首先是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键。如何使用@符号访问密钥?谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

答案发现于:Parsing JSON w/ @ at sign symbol in it (arobase)