简体   繁体   English

用符号符号解析JSON w / @(arobase)

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

My JSON object evaluates to: 我的JSON对象的计算结果为:

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

If this variable is called my_json , how do I access the @type value of XXX? 如果此变量名为my_json ,如何访问XXX的@type值? I tried my_json.@type , but this is giving errors. 我尝试了my_json.@type ,但这是错误的。 Help appreciated. 帮助赞赏。 Thanks, 谢谢,

Nick 缺口

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. 正如您所发现的,您不能在Javascript变量名称my_json.@type使用@符号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. 希望有所帮助。

If it ends up evaluating you can take the object and probably grab it by the key. 如果它最终评估你可以拿走对象并可能通过密钥抓住它。

ie obj["@type"]. 即obj [“@ type”]。 But something does seem a bit off. 但似乎有些事情似乎有点过时了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM