What are those @attributes thing I have in my JSON file and how can I read that with JQuery?
The JSON “text” I use is produced with json_encode in PHP from an array of custom objects.
Here’s a reduced JSON file:
{ "movies" : [ { "url":"http:\/\/www.youtube.com\/watch?v=Nsd7ZcXnL6k", title":{"@attributes":{"type":"text"},"0":"**Title here**"} ] }
I can read the URL easily with the following code :
$.getJSON(url, function(json){
$.each(json.movies,function(i,item) {
alert(item.url);
});
});
How can I read the title Title here value ?
UPDATE
Well, I still don’t know what the @attributes are, but I know why they were in my final JSON file. I was using $sxml = simplexml_load_file($feedURL); to read a XML and then $sxml->title to read a title, wich is not a string apparently but some kind of PHP object.
Instead of
$this->title = $sxml->title
I used
$this->title = $sxml->title . ""
(Ot’s converting the object into a string value). Maybe there’s a more intelligent way of doing this?
If you have a recent PHP, it supports casting, so you can use
(string)$xml->title
and it’ll work.
1 Answer