如何在JSON中进行带变量替换的查询

huangapple go评论65阅读模式
英文:

How make query in JSON with variable substitution

问题

I have JSON file like this (example):

{
    "deecracks": [
        {
            "layercodename": "osm",
            "layername": "Open Street Map"
        },
        {
            "layercodename": "olen",
            "layername": "tracks"
        }
    ],

    "theboomtownrats": [
        {
            "layercodename": "kuku",
            "layername": "Open Street Map"
        },
        {
            "layercodename": "olen",
            "pathto": "rerere.json"
        }
    }
}

I can get the value I need:

var megatest = data.theboomtownrats[0].layercodename;

This will be kuku.

But theboomtownrats or deecracks or other values like this are contained in a variable. If I write just data.theboomtownrats[0].layercodename in the code, everything works. However, if I do something like this:

var testmega = 'data.' + ${artist} + '[0].layercodename';

or just concatenate text and a variable, I get the same string in the variable testmega.

Is it possible to substitute the value of a variable in the query? If possible, how?

p.s. I found that this can be done using eval(), and it works, but people say it's not safe and should be done using a function, but I'm having trouble with this.

英文:

I have JSON file like this (example):

{
"deecracks": [
{
"layercodename": "osm",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"layername": "tracks",
}
],
      
"theboomtownrats": [
{
"layercodename": "kuku",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"pathto": "rerere.json",
}
]
}

I can get value what me need:

var megatest = data.theboomtownrats[0].layercodename;

This will be kuku

But theboomtownrats or deecracks or other value like this contain in variable.
If i in code write just data.theboomtownrats[0].layercodename all is work, but if i do like this:

var testmega = `'data.' + ${artist} + '[0].layercodename'`

or just use concatenation text and variable i get just same string in var testmega.

Its possible - substitution value of variable in query? If possible - how?

p.s. I found - this can make over the eval() and this work, but people say it's not safe, and must do over the function, but i have failute with this.

Evaluate an Equation in Javascript, without eval()

如何在JSON中进行带变量替换的查询

答案1

得分: 1

你可以使用 JavaScript 对象的方括号表示法并向它们传递一个变量。

所以,不需要用句点分隔,只需将字符串包装在方括号中。然后,你可以在方括号中传递一个变量。

let artist = "deecracks";
console.log(data[artist][0]["layercodename"])
let data = {
  "deecracks": [{
      "layercodename": "osm",
      "layername": "Open Street Map",
    },
    {
      "layercodename": "olen",
      "layername": "tracks",
    }
  ],

  "theboomtownrats": [{
      "layercodename": "kuku",
      "layername": "Open Street Map",
    },
    {
      "layercodename": "olen",
      "pathto": "rerere.json",
    }
  ]
}

let artist = "deecracks";
console.log(data[artist][0]["layercodename"])

请注意,这是你提供的代码的翻译。

英文:

You can use bracket notation with javascript objects and pass a variable to them.

So instead of separating with a period, just wrap the string in brackets. And you can pass a variable in the brackets.

let artist = "deecracks";
console.log(data[artist][0]["layercodename"])

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let data = {
  &quot;deecracks&quot;: [{
      &quot;layercodename&quot;: &quot;osm&quot;,
      &quot;layername&quot;: &quot;Open Street Map&quot;,
    },
    {
      &quot;layercodename&quot;: &quot;olen&quot;,
      &quot;layername&quot;: &quot;tracks&quot;,
    }
  ],

  &quot;theboomtownrats&quot;: [{
      &quot;layercodename&quot;: &quot;kuku&quot;,
      &quot;layername&quot;: &quot;Open Street Map&quot;,
    },
    {
      &quot;layercodename&quot;: &quot;olen&quot;,
      &quot;pathto&quot;: &quot;rerere.json&quot;,
    }
  ]
}

let artist = &quot;deecracks&quot;;
console.log(data[artist][0][&quot;layercodename&quot;])

<!-- end snippet -->

答案2

得分: 1

尝试使用方括号表示法。像这样:

var artist = 'theboomtownrats';
var megatest = data[artist][0].layercodename;
英文:

Try the bracket notation. Like this:

var artist = &#39;theboomtownrats&#39;;
var megatest = data[artist][0].layercodename;

huangapple
  • 本文由 发表于 2023年4月6日 23:22:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定