16

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"
 }
 ]
}
Colin Hebert
  • 88,795
  • 14
  • 155
  • 148
AutoMEta
  • 1,289
  • 5
  • 22
  • 33
  • I can't understand what your problem is. You may try explaining yourself in a simpler way... give some examples. – Luca Matteis Oct 05 '10 at 11:32
  • 2
    Java isn't Javascript ;) – Colin Hebert Oct 05 '10 at 11:34
  • @ Luca Matteis Douglas has answered the question..Actually i was unknown to the syntax for getting the value from key.instead of data.(inputLocation.value) ,i used data[inputLocation] and the problem resolved. – AutoMEta Oct 05 '10 at 11:41

2 Answers2

57

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
Douglas
  • 35,066
  • 8
  • 72
  • 89
0

get value from dynamic json object Using real time currency converter rest api responce

public async Task<JsonResult> ConvertCurrency(float Price, string FromCurrency)
{
    var testcase = FromCurrency + "_USD";
    WebClient web = new WebClient();
    const string ConverterApiURL = "http://free.currencyconverterapi.com/api/v5/convert?q={0}_{1}&compact=ultra&apiKey={{EnterKey}}";
    string url = String.Format(ConverterApiURL, FromCurrency, "USD");

    string response = new WebClient().DownloadString(url);

    var data = (JObject)JsonConvert.DeserializeObject(response);
    dynamic result = data.SelectToken(testcase + ".val").ToString();

    var basePrice = float.Parse(result);

    double exchangeRate = Price * basePrice;
    var responce = Math.Round(exchangeRate, 2);
    return Json(responce, JsonRequestBehavior.AllowGet);
}
Yasin Sunni
  • 274
  • 7
  • 9
  • Why have you posted a C# answer on a question asking about JS? Also even in C# what is the need to show how to use a specific API? You could have just said that there is a `SelectToken` method. – brc-dd Aug 03 '21 at 06:21