即使返回,JSON reviver函数也会返回undefined

时间:2017-04-20 14:25:11

标签: javascript json reviver-function

function _quotedText(data, config) {
    var pathKeys=config.customKey;  
    console.log(pathKeys);         //prints os_platform
    var inAuth_data=(JSON.parse(JSON.parse(JSON.parse(data["event.fields.custom_fields.inauth_device_data"])), (key, value) => {
        if (key == pathKeys)
        {
            console.log(value);     //prints Android
            return value;           //returns undefined
        }
    }));

    console.log(inAuth_data);       //prints undefined
    return inAuth_data;             //returns undefined
}

我已经阅读了其他类似的问题,并尝试了他们正在做的事情,但我仍然没有得到它。为什么它返回undefined?

这里讨论的JSON是:

"\"{\\\"deviceInfo\\\":{\\\"permanentId\\\":\\\"23434433-3333-4444-9581-f9cb641d28f5\\\",\\\"publicSigningKey\\\":\\\"MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAzMRW6jXVgqX0QV0EA9h2XnnPvntER5yqPKvD+yLKtxXzBCYMzygEM1nlwBRZhVpNJFvzZ2X+oTLGasdbasjhddasdu0PAWIc1AqKMt6rDEJv4a8bgqqAnXXnvR/QjwtsIq3T59LqivcoB2IPGq7Mof7yRJXKtrEOK2a1b8ixWJ5MBZ06drONhMkzeDTKjenMSM0Hf3BFTlXKCFaZbfShr1OK+wMqWFYrAJsAsufvzwjxKzaZ/RspVuZtfHo0g0z7SxWRBT7+2lKGN0pFyUYebN471n5hZxVMY8Zjfr75QUK1fWIVhzNRc+pH3PhakBeqsnmNncy+XOA7TwIBEQ==\\\"},\\\"objects\\\":[{\\\"perm_id\\\":\\\"READ_PHONE_STATE_DISABLED\\\",\\\"device_pid\\\":\\\"READ_PHONE_STATE_DISABLED\\\",\\\"sdk_version\\\":\\\"Android-MME-7.8.2\\\",\\\"data\\\":{\\\"contact_info_logs\\\":[],\\\"wifi_connection_logs\\\":[{\\\"ip\\\":\\\"111.22.33.44\\\",\\\"ssid\\\":\\\"\\\\\\\"iPhone sdf Gucci\\\\\\\"\\\",\\\"linkspeed\\\":\\\"72\\\",\\\"bssid\\\":\\\"22: 56: 23: 45: 0c: ab\\\",\\\"rssi\\\":\\\"-35\\\",\\\"macaddr\\\":\\\"a4: ss: ea: 17: dg: ss\\\",\\\"networkid\\\":\\\"17\\\"}]}}]}\""

1 个答案:

答案 0 :(得分:0)

经过大量的敲击后找到了答案。 但是,似乎正在为JSON中的每个对象调用该函数。之后

  

if(key == pathKeys)

检查它打印了值并返回了值,但匹配键后的JSON字符串中仍有对象被覆盖到变量inAuth_data中。

因此,我必须在reviver函数之外声明另一个变量,并在条件为真时为其赋值。

function _quotedText(data, config) {
var pathKeys=config.customKey;  
console.log(pathKeys);         //prints os_platform
var data = undefined;
var inAuth_data =(JSON.parse(JSON.parse(JSON.parse(data["event.fields.custom_fields.inauth_device_data"])), (key, value) => {
    if (key == pathKeys)
    {
        console.log(value);     //prints Android
        data = value;           //Assigning value of value to data
    }
  }));

console.log(inAuth_data);       //prints Android
return inAuth_data;             //returns Android

}

感谢@evolutionxbox与我合作。