gpt4 book ai didi

javascript - 我可以使用预设变量放置 json 调用字符串吗?

转载 作者:行者123 更新时间:2023-12-02 18:35:34 26 4
gpt4 key购买 nike

<?php
date_default_timezone_set('America/Los_Angeles');
$time = date('Gi', time());
$day = date('l', time());
?>

<script type="text/javascript">
$(function() {
$(".show_hide").click( function()
{
var locTime = <?php echo json_encode($time) ?>;
var locDate = <?php echo json_encode($day) ?>;

$.getJSON( "url_to_json", function(data) {
for (var i = 0; i < data.location[locDate].length; i++) {
console.log("sucess1");
xr_mon= data.location.locDate[i];
console.log("sucess2");
if (locTime >= xr_mon.kai && locTime < xr_mon.guan ){
console.log("cafe is open!");
$('.xr').show();
break;
}


else {
console.log("cafe is closed");
$('.xr').hide();
}
}
});
}
);
});

JSON

{ "location": 
{
"Monday": [
{"kai": 700, "guan": 1400},
{"kai": 1700, "guan": 2100}
],

"Tuesday": [
{"kai": 700, "guan": 1400},
{"kai": 1700, "guan": 2100}
],

"Wednesday": [
{"kai": 700, "guan": 1400},
{"kai": 1700, "guan": 2100},
{"kai": 2200, "guan": 2400},
{"kai": 0, "guan": 200}
],

"Thursday": [
{"kai": 700, "guan": 1400},
{"kai": 1700, "guan": 2100},
{"kai": 2200, "guan": 2400},
{"kai": 0, "guan": 200}
],

"Friday": [
{"kai": 700, "guan": 1400},
{"kai": 1700, "guan": 2100},
{"kai": 2200, "guan": 2400},
{"kai": 0, "guan": 200}
],

"Saturday": [
{"kai": 1000, "guan": 1500},
{"kai": 1700, "guan": 2100},
{"kai": 2200, "guan": 2400},
{"kai": 0, "guan": 200}
],

"Sunday": [
{"kai": 1000, "guan": 1500},
{"kai": 1700, "guan": 2100},
{"kai": 2200, "guan": 2400},
{"kai": 0, "guan": 200}
]
}
}

其中 kai = 开放时间和 guan = 关门时间

locDate 是通过 php 服务器端日期提取的,我想替换用我的 JSON 调用字符串生成的日期,该字符串包含工作日所在的部分。

从这里

  for (var i = 0; i < data.location.'locDate'.length; i++)

到此

  for (var i = 0; i < data.location.Thursday.length; i++)

我该如何以正确的方式做到这一点。有人建议使用 [locDate],但它不会替换 php 日期数据中的变量。

最佳答案

使用括号表示法。

for (var i = 0; i < data.location[locDate].length; i++)

另一个例子:

thing = [1, 2, 3, 4, 5]
//The following lines do the same thing:
thing.pop()
thing['pop']()

关于javascript - 我可以使用预设变量放置 json 调用字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17354609/

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