Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");

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

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;

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

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

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

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"
 }
 ]
}
like image 624
AutoMEta Avatar asked Oct 05 '10 11:10

AutoMEta


People also ask

How do I get key-value pairs in JSON?

In order to get a key-value pair from a KiiObject, call the get() method of the KiiObject class. Specify the key for the value to get as the argument of the get() method. The value of the key at the first level of the JSON document hierarchy will be obtained.

How does JSON use key-value pairs?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.


1 Answers

You can access the property using array-like syntax:

data[inputLocation]

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

data["Aspen"]
data.Aspen
like image 76
Douglas Avatar answered Sep 19 '22 05:09

Douglas