fannheyward
fannheyward

Reputation: 19277

CoffeeScript get key/value error from JSON

Here is my json string:

{
    "long-url":"http://a.longlongurl.com";
}

parse json string:

var jsonObj = JSON.parse json_str
alert(jsonObj.long-url);

error: ReferenceError: url is not defined. Looks like - in the key make this error. How to get value if the key has a minus in it?

Thanks.

Upvotes: 0

Views: 940

Answers (1)

SLaks
SLaks

Reputation: 887459

Your subtracting url from jsonObj.long.
That's not what you want to do.

Instead, use indexer notation:

jsonObj["long-url"]

Upvotes: 4

Related Questions