遍历MySQL/MariaDB返回的JSON时TIMESTAMP字段LogDate被忽略的解决方法
问题描述
我需要遍历MySQL(MariaDB)返回的JSON格式数据,提取所有键值对用于后续生成HTML表格。目前用递归遍历函数处理时,发现数据库中timestamp类型的LogDate字段被忽略,控制台完全没输出该字段的信息。
数据库返回的数据结构
[ { ID: 5, UID: 'E123456', LogDate: 2023-03-24T12:55:25.000Z, SensorName: 'Sensor1', SensorValue: '20.01' }, { ID: 6, UID: 'E123456', LogDate: 2023-03-24T12:55:45.000Z, SensorName: 'Sensor1', SensorValue: '21.20' } ]
递归遍历函数
function iterateRecursively(object, callbackFunction) { // Walk through object for (var keyName in object) { // If the given parameter is an object, call the function over and // over again, till you get a string or number if (typeof object[keyName] === 'object') { console.log("Object - Typeof : " + typeof(keyName)); iterateRecursively(object[keyName], callbackFunction); } else { // Callback function to do something with results outside of main function if (typeof callbackFunction === 'function') { callbackFunction(keyName, object[keyName]); } } } }
数据库请求及日志代码
let data = await db.getSensorData(FzID); iterateRecursively(data, function (key, value) { console.log(key + " = " + value + "(" + typeof(key) + " : " + typeof(value) + ")"); });
控制台输出
Object - Typeof : string ID = 5(string : number) UID = E123456(string : string) Object - Typeof : string SensorName = Sensor1(string : string) SensorValue = 20.01(string : string) Object - Typeof : string ID = 6(string : number) UID = E123456(string : string) Object - Typeof : string SensorName = Sensor1(string : string) SensorValue = 21.20(string : string)
问题原因及解决方案
原因分析
LogDate对应的是JavaScript的Date对象,typeof Date的结果是object,所以你的递归函数会进入typeof object[keyName] === 'object'分支,尝试递归遍历Date对象的内部属性,但Date对象的可枚举属性里没有能触发回调的键值,最终导致该字段被跳过,没输出到控制台。
另外,输入的data是数组,typeof 数组也是object,所以函数会先递归遍历数组元素,这也是控制台先输出Object - Typeof : string的原因(数组索引是字符串类型的键)。
解决方法
修改递归函数,增加对Date对象的判断:如果是Date类型,直接调用回调处理,不再递归。同时排除null(因为typeof null也是object,避免误处理)。
修改后的遍历函数
function iterateRecursively(object, callbackFunction) { for (var keyName in object) { const value = object[keyName]; // 基础类型、Date、null直接触发回调 if (typeof value !== 'object' || value === null || value instanceof Date) { typeof callbackFunction === 'function' && callbackFunction(keyName, value); } else { console.log("Object - Typeof : " + typeof(keyName)); iterateRecursively(value, callbackFunction); } } }
可选:格式化Date输出
如果需要把Date转为可读性更好的字符串,可在回调里处理:
iterateRecursively(data, function (key, value) { const displayValue = value instanceof Date ? value.toISOString() : value; console.log(key + " = " + displayValue + "(" + typeof(key) + " : " + typeof(value) + ")"); });
修改后控制台会输出LogDate字段,比如:
Object - Typeof : string ID = 5(string : number) UID = E123456(string : string) LogDate = 2023-03-24T12:55:25.000Z(string : object) SensorName = Sensor1(string : string) SensorValue = 20.01(string : string) Object - Typeof : string ID = 6(string : number) UID = E123456(string : string) LogDate = 2023-03-24T12:55:45.000Z(string : object) SensorName = Sensor1(string : string) SensorValue = 21.20(string : string)
内容的提问来源于stack exchange,提问作者Ringo
相关产品推荐
相关产品推荐

