gpt4 book ai didi

javascript - 在 JSON.parse 中使用变量

转载 作者:行者123 更新时间:2023-12-02 22:21:33 25 4
gpt4 key购买 nike

我想使用变量从 JSON 中获取特定值。

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var jsonContent = JSON.parse(this.responseText);

var day = mon;
var name = abc;

var restaurants = jsonContent.name.table.day;

每次代码启动时,变量“day”和“name”都会不同,因为我需要 JSON 中的不同值。

我该如何解决这个问题?

最佳答案

使用这样的属性字符串获取值

var restaurants = jsonContent[name].table[day]

关于javascript - 在 JSON.parse 中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com