[英]Cannot access Object key “@attributes” for itunes RSS feed
我试图通过通过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键。 如何使用带有@符号的键? 谢谢!
解决方案是使用括号符号访问该对象。 我添加到代码中的是
var enclosure = dataObject.channel.item[0].enclosure;
console.log(enclosure["@attributes"]);
在以下位置找到了答案:在其中解析符号w / @的JSON(arobase)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.