HectorGuo
HectorGuo

Reputation: 1101

javascript: how to cope with "@" and ":" in Json

There is a json data:

var json = {  
    "entry" : [{ 
        "category" : {
            "@scheme" : "http:\/\/www.douban.com\/2007#kind", 
            "@term" : "http:\/\/www.douban.com\/2007#movie"
        }, 
        "title" : {
            "$t" : "Real Steel"
        },
        "db:attribute" : [{
            "$t" : "USA", 
            "@name" : "country"
        }]
    }]
}

alert(json.entry[0].title.$t) can print "Real Steel". However, how to get the http:\/\/www.douban.com\/2007#kind and USA in Javascript ? 

 It contains @ and :, I don't know how to cope with it.

Upvotes: 1

Views: 126

Answers (2)

James Montagne
James Montagne

Reputation: 78650

If I'm understanding you correctly, you can do the following:

json.entry[0].category["@scheme"]

Upvotes: 1

user1106925
user1106925

Reputation:

Use square brackets and a string instead of .

json.entry[0].category["@scheme"]
json.entry[0].category["@term"]
json.entry[0]["db:attribute"][0].$t
json.entry[0]["db:attribute"][0]["@name"]

Upvotes: 6

Related Questions