Javascript 解析带有 @ 符号的 JSON (arobase)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6932745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:49:55  来源:igfitidea点击:

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

javascriptjsontitaniumat-sign

提问by user823596

My JSON object evaluates to:

我的 JSON 对象评估为:

{ "@io": IO, "@type": XXX }

If this variable is called my_json, how do I access the @typevalue of XXX? I tried my_json.@type, but this is giving errors. Help appreciated. Thanks,

如果调用此变量my_json,我如何访问@typeXXX的值?我试过my_json.@type,但这是给出错误。帮助表示赞赏。谢谢,

Nick

缺口

回答by JAAulde

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];

回答by Spudley

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

正如您所发现的,您不能在 Javascript 变量名称中使用 @ 符号,这my_json.@type是无效的。

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.

希望有帮助。

回答by ek_ny

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.

即 obj["@type"]。但似乎有些不对劲。