Parsing JSON w/ @ at sign symbol in it (arobase)

有些话、适合烂在心里 提交于 2019-11-27 08:51:58

Use square bracket notation with a string:

var XXXValue = my_json['@type'];

The same can be used when you have a property name in a variable. Using your same example:

var propertyName = '@type';
var XXXValue = my_json[propertyName];

As you've discovered, you can't use an @ symbol in a Javascript variable name, my_json.@type is invalid.

The good news for you is that you can access your variables as array subscripts. You would do it like this:

my_json["@type"]

Hope that helps.

If it ends up evaluating you can take the object and probably grab it by the key.

ie obj["@type"]. But something does seem a bit off.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!