从json中的动态键值获取数据

发布于 2024-09-26 00:22:04 字数 807 浏览 1 评论 0原文

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

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

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

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

var inputLocation=input.value;

在我的 javascript 中,如果我使用动态键,则会收到错误。

如果我执行此操作,我可以获得 json 数组 data.Aspen 但我必须从文本字段获取数据,并且它可能会有所不同,因此如果我调用 data.inputLocation... 当我使用 data.(inputLocation.value) 时未定义

,出现以下错误:

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"
 }
 ]
}

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"
 }
 ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-10-03 00:22:04

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

data[inputLocation]

如果 inputLocation 设置为 "Aspen",则与这两行相同:

data["Aspen"]
data.Aspen

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
游魂 2024-10-03 00:22:04

从动态 json 对象获取值 使用实时货币转换器 REST API 响应

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文