views:

391

answers:

1

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 . "" (it'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.

A: 
$.getJSON(url, function(json){

    $.each(json.movies,function(i,item) {
        alert(item.url); 
        alert(item.title[0]); 
    });
 });
PetersenDidIt
Ah, that's what that 0 is for! Thanks! ;)