简体   繁体   中英

Cannot access Object key “@attributes” for itunes RSS feed

I'm trying to parse an itunes RSS feed by converting it to JSON via php and then inserting it with jQuery .ajax.

First is the 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;
?>

Now the 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");
}
});

What that logs out is

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

My only issue here is accessing the @attributes key. How do I access a key with an @ symbol? Thanks!

The solution is to access the object with bracket notation. What I've added to my code is

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

Answer was found at : Parsing JSON w/ @ at sign symbol in it (arobase)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM