gpt4 book ai didi

javascript - 从 JSON 中获取列名

转载 作者:行者123 更新时间:2023-11-30 08:34:37 25 4
gpt4 key购买 nike

我有以下示例 JSON,我想获取列的名称,例如 Dribbble、Behance ...

{ 
status: 200,
success: true,
result:
[ { Dribbble: 'a',
Behance: '',
Blog: 'http://blog.invisionapp.com/reimagine-web-design-process/',
Youtube: '',
Vimeo: '' },
{ Dribbble: '',
Behance: '',
Blog: 'http://creative.mailchimp.com/paint-drips/?_ga=1.32574201.612462484.1431430487',
Youtube: '',
Vimeo: '' } ]
}

我正在使用 Request 模块,它可以很好地返回 JSON,但我正在努力获取字符串格式的列名。如果我至少能得到数字格式的列,我就能知道它是否是正确的列,但我得到的却是两个 0。

request({
url: url,
json: true
}, function (error, response, body) {

if (!error && response.statusCode === 200) {
callback(body)
var json = body.result;

for (var key in json) {
if (json.hasOwnProperty(key)) {
var column = Object.keys(key);
console.log(column[0]);
}
}

}
})

最佳答案

var json = { 
"status": 200,
"success": true,
"result": [
{
"Dribbble": 'a',
"Behance": '',
"Blog": 'http://blog.invisionapp.com/reimagine-web-design-process/',
"Youtube": '',
"Vimeo": ''
},
{
"Dribbble": '',
"Behance": '',
"Blog": 'http://creative.mailchimp.com/paint-drips/?_ga=1.32574201.612462484.1431430487',
"Youtube": '',
"Vimeo": '' }
]
}
// get the results (useful data) somewhere
var results = json["result"];
// get the first result set, or you can loop trhrough all, assuming that each reuslt set is the same.
if (results.length > 0){
var columnsIn = results[0];
for(var key in columnsIn){
console.log(key); // here is your column name you are looking for
}
}else{
console.log("No columns");
}

此代码片段应为您指明正确的方向

关于javascript - 从 JSON 中获取列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32854861/

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