0

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!

thatgibbyguy
  • 4,033
  • 3
  • 23
  • 39

1 Answers1

0

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)

Community
  • 1
  • 1
thatgibbyguy
  • 4,033
  • 3
  • 23
  • 39