从json中的动态键值获取数据 [英] get data from dynamic key value in json
问题描述
要求如下:
我必须从页面获取位置字段。
The requirement is following:
I have to get the location field from page.
var input= global.input = document.getElementById("Location");
根据输入并在页面上显示,从json文件中获取邻域区域。
Get the neighborhood area from the json file based on input and show on the page.
我有一个json对象,必须根据键值(位置)过滤json对象的数据
I have a json object and have to filter the data from the json object based on the key value (location)
var inputLocation=input.value;
在我的javascript中,如果我使用动态密钥,我会收到错误。
In my javascript I am getting the error if I use dynamic the key.
如果我这样做,我能够获得json数组 data.Aspen
但是我必须从文本字段中获取数据并且它可以是不同的,所以如果我调用data.inputLocation ...它来的未定义
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
当我使用数据。(inputLocation.value)
收到以下错误:
when i use data.(inputLocation.value)
getting the following error :
XML过滤器应用于非XML值({Aspen:[{ID:
XML filter is applied to non-XML value ({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"
}
]
}
推荐答案
您可以使用arra访问该物业y-like语法:
You can access the property using the array-like syntax:
data[inputLocation]
如果 inputLocation
设置为Aspen
,这是与这两行相同:
If inputLocation
is set to "Aspen"
, this it is the same as these two lines:
data["Aspen"]
data.Aspen
这篇关于从json中的动态键值获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!