获取 JSON 对象内的 typeof 元素
Posted
技术标签:
【中文标题】获取 JSON 对象内的 typeof 元素【英文标题】:Get typeof element inside of JSON object 【发布时间】:2020-07-18 17:24:32 【问题描述】:我有一个 JSON 文件,在 @Supercool 的帮助下,我能够获取每个文件的键名。我希望能够获得值的类型。
JSON 结构:
[
"id": 1536700,
"title": "final_output",
"error": "",
"data": [
"metric": 4940616.0,
"title": "d_revenue"
,
"metric": 5132162.0,
"title": "p_revenue"
,
"metric": 4954576.0,
"title": "s_revenue"
,
"metric": 4882217.0,
"title": "u_revenue"
,
"metric": 4869609.0,
"title": "t_revenue"
,
"metric": 5075422.0,
"title": "w_revenue"
,
"metric": 4461996.0,
"title": "v_revenue"
]
]
下一个结构:
[
"run_id": 1536700,
"code_title": "select_data",
"error": "",
"data": [
"user_name": "C_51",
"num1": 51,
"num2": 101,
"num3": 151
,
"user_name": "H_51",
"num1": 51,
"num2": 101,
"num3": 151
,
"user_name": "C_52",
"num1": 52,
"num2": 102,
"num3": 152
,
"user_name": "H_52",
"num1": 52,
"num2": 102,
"num3": 152
,
"user_name": "C_53",
"num1": 53,
"num2": 103,
"num3": 153
]
]
使用 Object.keys(yourObject) 获取密钥
$.getJSON(api, function(elem)
let keys=elem.map( structure => Object.keys(structure.data[0]))
);
运行下面的sn-p来检查它是否有效
let ele= ["id": 1536700,"title": "final_output","error": "",
"data": ["metric": 4940616.0,"title": "d_revenue","metric": 5132162.0,"title": "p_revenue","metric":4954576.0,"title": "s_revenue","metric": 4882217.0,"title":"u_revenue","metric": 4869609.0,"title":"t_revenue","metric": 5075422.0,"title": "w_revenue","metric": 4461996.0,"title": "v_revenue"
]
,
"run_id": 1536700,"code_title": "select_data","error": "",
"data": ["user_name": "C_51","num1": 51,"num2": 101,"num3": 151,"user_name": "H_51","num1": 51,"num2": 101, "num3": 151,"user_name": "C_52","num1": 52,"num2": 102,"num3": 152,"user_name": "H_52","num1": 52,"num2": 102,"num3":152,"user_name": "C_53","num1": 53,"num2": 103,"num3": 153
]]
console.log(ele.map( structure => Object.keys(structure.data[0])))
console.log(ele.map( structure => Object.values(structure.data[0])))
.as-console-wrapper max-height: 100% !important; top: 0;
我希望能够获取每个值的类型。对于上面列出的值,我可以得到这个吗?
[
[
number,
string
],
[
string,
number,
number,
number
]
]
【问题讨论】:
保留密钥不是更有意义吗? @RoboRobok,你是什么意思? 您可以将其转换为将值替换为类型的对象,而不是将其保存在数组中。虽然不确定你需要它做什么,但我几乎 100% 肯定你做了一些有趣的事情。 @Supercool 对象键在 javascript 中始终是字符串。ele.map(structure => Object.values(structure.data[0]).map(v => typeof v))
【参考方案1】:
let ele = ["id": 1536700,"title": "final_output","error": "", "data": ["metric": 4940616.0,"title": "d_revenue","metric": 5132162.0,"title": "p_revenue","metric":4954576.0,"title": "s_revenue","metric": 4882217.0,"title":"u_revenue","metric": 4869609.0,"title":"t_revenue","metric": 5075422.0,"title": "w_revenue","metric": 4461996.0,"title": "v_revenue"], "run_id": 1536700,"code_title": "select_data","error": "", "data": ["user_name": "C_51","num1": 51,"num2": 101,"num3": 151,"user_name": "H_51","num1": 51,"num2": 101, "num3": 151,"user_name": "C_52","num1": 52,"num2": 102,"num3": 152,"user_name": "H_52","num1": 52,"num2": 102,"num3":152,"user_name": "C_53","num1": 53,"num2": 103,"num3": 153]];
console.log(ele.map(structure => Object.values(structure.data[0]).map(v => typeof v)));
【讨论】:
请记住,JavaScript 并不能保证每种情况下的对象属性顺序。 使用地图将保证它***.com/questions/5525795/… @Supercool。map
不保证财产顺序,它是对 Object.values()
的调用。【参考方案2】:
通过 Object.values(obj) 做一个map()
并使用typeof
值返回类型
let ele= ["id": 1536700,"title": "final_output","error": "",
"data": ["metric": 4940616.0,"title": "d_revenue","metric": 5132162.0,"title": "p_revenue","metric":4954576.0,"title": "s_revenue","metric": 4882217.0,"title":"u_revenue","metric": 4869609.0,"title":"t_revenue","metric": 5075422.0,"title": "w_revenue","metric": 4461996.0,"title": "v_revenue"
]
,
"run_id": 1536700,"code_title": "select_data","error": "",
"data": ["user_name": "C_51","num1": 51,"num2": 101,"num3": 151,"user_name": "H_51","num1": 51,"num2": 101, "num3": 151,"user_name": "C_52","num1": 52,"num2": 102,"num3": 152,"user_name": "H_52","num1": 52,"num2": 102,"num3":152,"user_name": "C_53","num1": 53,"num2": 103,"num3": 153
]]
console.log("Keys are ..");
console.log(ele.map( structure => Object.keys(structure.data[0])))
console.log("and their correspondent value types are ...");
console.log(ele.map( structure => Object.values(structure.data[0]).map(value => typeof value)))
.as-console-wrapper max-height: 100% !important; top: 0;
【讨论】:
【参考方案3】:Javascript 是一种原型语言,所以你可以在 js 中的任何结构上使用constructor.name
属性来查看它是什么类型。
简单示例:
const a = t: 5, s: '', d: false, e:[], f:, d:5.53 ;
for (let k in a)
console.log(k,a[k].constructor.name);
【讨论】:
做了一个sn-p并添加了密钥,因为它没有按顺序登录 map会保证***.com/questions/5525795/…以上是关于获取 JSON 对象内的 typeof 元素的主要内容,如果未能解决你的问题,请参考以下文章
java 为jersery添加json支持(jersery实际上将对象转换为XML然后转换为JSON,以下示例为media typ