简体   繁体   中英

Json call invalid due to @symbol

I validated the Json online and seems to be valid. I called the json array key but editor throwed Parsing error: Unexpected character '@'

The editor is Visual studio code and code seems not be working due to @ symbol

<script>
{
    "@context": "https://example.org",
    "@graph": [
        {
            "@type": "Organization",
            "@id": "https://examples.com/#organization",
            "name": "",
            "url": "https://examples.com/",
            "sameAs": []
        }
    ]
}
var x = @context;
var y = @graph[0].@type;
console.log(x);
console.log(y);
</script>

Need to console log the values

As per my comment, you need to first assing your object to a variable first. From there, you can then access the object key-value pairs by using the square bracket notation.

You cannot use @ character in dot notation. If you want to access the value defined by the key @context , you need to use d['@context'] and not d.@context .

 var d = { "@context": "https://example.org", "@graph": [ { "@type": "Organization", "@id": "https://examples.com/#organization", "name": "", "url": "https://examples.com/", "sameAs": [] } ] } var x = d['@context']; var y = d['@graph'][0]['@type']; console.log(x); console.log(y);

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