ezhil
ezhil

Reputation: 1083

Issue while parsing JSON response

I need to parse JSON value. I can get 'name' value but I am unable to retrieve json value if the property has colon(:). PFB the code

<script>
var text = '{"name": "james","age": "30","re:moto" : "Hi"}'

var obj = JSON.parse(text);

document.getElementById("demo").innerHTML =
obj.name + "<br>" + obj.age ;
</script>

Here I am getting name value as 'james'. Giving error while retrieving 're:moto'. Can any one tell me how to retrieve property if it has colon?

Upvotes: 0

Views: 54

Answers (1)

epascarello
epascarello

Reputation: 207511

You would need to use bracket notation.

var text = '{"name": "james","age": "30","re:moto" : "Hi"}';
var obj = JSON.parse(text);
console.log(obj["re:moto"]);

Upvotes: 5

Related Questions