Javascript 从 JSON 获取列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32854861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:17:22  来源:igfitidea点击:

Get column name from JSON

javascriptjson

提问by Seong Lee

I have the following sample JSON and I would like to get the name of the column like Dribbble, Behance ...

我有以下示例 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: '' } ] 
}

I'm using Request module and it returns JSON fine but I'm struggling to get the column name in string format. If I could get the column in number format at least, I would be able to know if it's the right column but I get two 0s instead.

我正在使用 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]); 
            }
        }

    }
})

回答by Chad Peruggia

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");
}

This code snippet should point you in the proper direction

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

回答by Grassycup

Going off of Chad Peruggia's code, you can print out every key value pair in the results array by doing this:

从 Chad Peruggia 的代码开始,您可以通过执行以下操作打印出结果数组中的每个键值对:

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"];
// you can loop through all, assuming that each result set is the same. 
if (results.length > 0) { 
    // iterating through the results array
    for(var i = 0; i < results.length; i++) {
        // get i-th object in the results array  
        var columnsIn = results[i];
        // loop through every key in the object
        for(var key in columnsIn){
            console.log(key + ' : ' + results[i][key]); // here is your column name you are looking for + its value
        } 
    }
}
else {
    console.log("No columns");
}