简体   繁体   English

使用变量调用 json object 点表示法

[英]Calling json object dot notation with a variable

Hi i have a json object to access the path is嗨我有一个 json object 访问路径是
data.Application.Exabeam

const target = data.Application.Exabeam

I have trouble, I would like to replace the "Exabeam" to be referenced by a variable我有麻烦,我想替换变量引用的“Exabeam”

I tried to add a variable as follows我尝试按如下方式添加变量

var casetypesession = sessionStorage.getItem("homepageclicksession");

a) const target = data.Application.casetypesession; a) const target = data.Application.casetypesession; (dosn't work because there is no casetypesession in my json object (不起作用,因为我的 json object 中没有casetypesession

b) const target=data.Applications.casetypesession.valueOf(); b) const target=data.Applications.casetypesession.valueOf(); (dosn't work, casetypesession is not even recognized in the variable in the dot notation) (不起作用,在点符号的变量中甚至无法识别casetypesession

The syntax for accessing the property of an object is:访问 object 的属性的语法是:

objectName.property         // person.age

or或者

objectName["property"]      // person["age"]

or或者

objectName[expression]      // x = "age"; person[x]

See this reference for details.有关详细信息,请参阅此参考

Which in your case translates to something like this:在您的情况下,这会转化为以下内容:

const target = data.Applications[casetypesession]

My own mistake, did not specify the array before the exabeam portion!我自己的错误,没有在 exabeam 部分之前指定数组!

target=data.Applications[0][casetypesession];目标=data.Applications[0][casetypesession];

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

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