gpt4 book ai didi

javascript - 函数无法正确读取值

转载 作者:行者123 更新时间:2023-12-03 08:52:49 24 4
gpt4 key购买 nike

JSON 的快照输出:

{"control_mode":0.0,"lvl_danger":0.0,"lvl_alert":0.0,"lvl_normal":0.0,"lvl_upStream":0.0,"lvl_warning"
:0.0,"lvl_downStream":0.0,"lvl_control":null,"rtu_supply":false,"elcb_locked":false,"red_phase":false,
"yellow_phase":false,"blue_phase":false,"gen_status":false,"gen_fail":false,"gen_lowFuel":false,"gen_lowOilPressure"
:false,"gen_lowOverSpeed":false,"gen_highTemp":false,"Gate1":{"gateNo":1,"fullOpen":false,"fullClose"
:false,"opening":false,"closing":false,"mode":0,"state":0,"cond":0,"positionM":0.0,"positionP":0.0}}

如您所见,通过此网址返回的 JSON 数据中的“state”:0:

    var RealDataApi = function () {
var url_getgatedata = "api/realdata/getgatedata";


// public functions
return {
getGateData: function (done, fail, always) {
var jqxhr = $.get(url_getgatedata);
jqXhrHandler(jqxhr, done, fail, always);
},

然后这个函数读取状态并根据状态值显示和隐藏图像。

     RealDataApi.getGateData(
function (data) {
if (data) {

$("#pDatetime").html(new Date().toLocaleString());


if (data.state == 0)
{
$("#gate1").show();
}
else if (data.state == 2)
{
$("#gate2").show();
}

这里的问题是函数无法正确读取状态值,并且不是通过url比较返回值,而是总是返回null?这里出了什么问题?

最佳答案

stateGate1 的属性。您需要执行data.Gate1.state(区分大小写,因此不要忘记大写的G)。

一个简单的检查方法是使用 JSON Linter ,这将为您很好地格式化所有内容。

简单的例子:

var data = {"control_mode":0.0,"lvl_danger":0.0,"lvl_alert":0.0,"lvl_normal":0.0,"lvl_upStream":0.0,"lvl_warning"
:0.0,"lvl_downStream":0.0,"lvl_control":null,"rtu_supply":false,"elcb_locked":false,"red_phase":false,
"yellow_phase":false,"blue_phase":false,"gen_status":false,"gen_fail":false,"gen_lowFuel":false,"gen_lowOilPressure"
:false,"gen_lowOverSpeed":false,"gen_highTemp":false,"Gate1":{"gateNo":1,"fullOpen":false,"fullClose"
:false,"opening":false,"closing":false,"mode":0,"state":0,"cond":0,"positionM":0.0,"positionP":0.0}}

console.log(data.Gate1.state == 0) // Outputs true

关于javascript - 函数无法正确读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623876/

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