从json中动态键值获取数据

时间:2022-12-04 15:24:54

The requirement is following:
I have to get the location field from page.

要求如下:我必须从页面获取位置字段。

var input= global.input = document.getElementById("Location");

Get the neighborhood area from the json file based on input and show on the page.

根据输入从json文件获取邻域并显示在页面上。

I have a json object and have to filter the data from the json object based on the key value (location)

我有一个json对象,必须根据键值(位置)从json对象中过滤数据

var inputLocation=input.value;

In my javascript I am getting the error if I use dynamic the key.

在javascript中,如果使用动态键,就会得到错误。

I am able to get the json array if I do this data.Aspen but i have to get the data from a text field and it can be different so if I call data.inputLocation... its coming undefined

如果我做这个数据,我可以得到json数组。但是我必须从文本字段中获取数据,它可以是不同的,所以如果我调用data. inputlocation。它来定义

when i use data.(inputLocation.value) getting the following error :

当我使用数据时。(inputlocation。value)得到以下错误:

XML filter is applied to non-XML value ({Aspen:[{ID:

XML过滤器用于非XML值({Aspen:[{ID:

{
 "Aspen":[
 {
  "ID":"Bellaire",
  "Name":"Bellaire"
 },
 {
  "ID":"Champions Forest",
  "Name":"Champions Forest"
 },
 {
  "ID":"Highland Village",
  "Name":"Highland Village"
 },
 {
  "ID":"Museum District",
  "Name":"Museum District"
 }
 ]
}

1 个解决方案

#1


37  

You can access the property using the array-like syntax:

您可以使用类似数组的语法访问该属性:

data[inputLocation]

If inputLocation is set to "Aspen", this it is the same as these two lines:

如果将inputLocation设置为“Aspen”,这与以下两行相同:

data["Aspen"]
data.Aspen

#1


37  

You can access the property using the array-like syntax:

您可以使用类似数组的语法访问该属性:

data[inputLocation]

If inputLocation is set to "Aspen", this it is the same as these two lines:

如果将inputLocation设置为“Aspen”,这与以下两行相同:

data["Aspen"]
data.Aspen