如何从json文件中检索所有字段?

时间:2016-04-25 11:20:19

标签: javascript json node.js

我想只检索字段名称而不是它们的值。

我目前的代码是:

    app.get("/api/borough_all_fields", function(req, res){
    // set the type of content in the response
    res.writeHead(200, {'Content-Type': 'application/json'});

    var borFld = null;

    // what to put in here?

    // add the results to an array
    respJson["results"] = [ borFld ];
    res.end( JSON.stringify( respJson ) );
});

另外如何指定字段名称并返回其所有记录的值?例如,指定GSS_code并返回所有值,仅用于gss代码及其名称?

2 个答案:

答案 0 :(得分:1)

您可以使用内置对象功能 如果您有这样的对象

var jsonObj = {"person":"me","age":"30"};
var borFld = Object.keys(jsonObj);  // returns ["person", "age"]

答案 1 :(得分:0)

您需要传递正确的json路径。

var borFld = Object.keys(json.features[0].properties); 

如果数组中有多个对象,则需要在数组上运行for循环并传递上面的行,它将返回所需的结果。