开发者

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

开发者 https://www.devze.com 2023-03-25 21:12 出处:网络
My JSON object evaluates to: { \"@io\": IO, \"@type\": XXX } If this vari开发者_如何学编程able is called my_json, how do I access the @type value of XXX? I tried my_json.@type, but this is giving e

My JSON object evaluates to:

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

If this vari开发者_如何学编程able is called my_json, how do I access the @type value of XXX? I tried my_json.@type, but this is giving errors. 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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消