Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

JSON Results JavaScript dot notation query returns undefined

I'm not the greatest at JavaScript but I'm learning. I have a wireless sensor and a smart plug I'm practicing with. For some reason I cannot get to the property relay_state. I've tried Dot and Bracket but both return undefined. I'd like it as a function like what I have below but I"m not sure what I am doing wrong. Basically I just want the code to retrieve the value for relay_state from the returned parsed data. Appreciate the feedback! Thanks.

acState = JSON.parse(returnValue).result.responseData;
                
                /*Returned response: 
                {
                  "system": {
                    "get_sysinfo": {
                      "sw_ver": "1.0.0",
                      "hw_ver": "1.0",
                      "type": "IOT.SMARTPLUGSWITCH",
                      "model": "MODELISHERE",
                      "mac": "MACIDISHERE",
                      "dev_name": "SMARTPLUG",
                      "alias": "PLUG ALIAS",
                      "relay_state": 0, //NEED THIS RETURNED
                      "on_time": 0,
                      "active_mode": "none",
                      "feature": "TIM",
                      "updating": 0,
                      "icon_hash": "",
                      "rssi": -53,
                      "led_off": 0,
                      "longitude_i": -LONGDATA,
                      "latitude_i": LATDATA,
                      "hwId": "HWIDISHERE",
                      "fwId": "00000000000000000000000000000000",
                      "deviceId": "DEVICEIDISHERE",
                      "oemId": "OEMIDISHERE",
                      "next_action": {
                        "type": -1
                      },
                      "err_code": 0
                    }
                  }
                }
                */
            
function value(layer, path, value) {
    var i = 0,
        path = path.split('.');

    for (; i < path.length; i++)
        if (value != null && i + 1 === path.length)
            layer[path[i]] = value;
        layer = layer[path[i]];

    return layer;
    console.Log("Relay State: " + value(acState,'system[get_sysinfo[relay_state]]]'));
};
console.Log("Response Data Parsed: " + acState);         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

what you have is an object and to access it you just need a point .. :)

var acState = JSON.parse(returnValue).result.responseData;
console.log(acState.system.get_sysinfo.relay_state);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...