如何在不知道json文件结构的情况下逐个读取nodejs中的对象

时间:2018-05-22 08:46:12

标签: json node.js

我的json文件数据:

<div id="Result1"></div>
<div id="Result2"></div>

如何在nodejs中读取这些数据,并使用属性[ {"name":"a","queryname":"Query_1","type":"user","context":"novell","searchsubcontainer":false}, {"name":"aa","queryname":"Query_2","type":"user","context":"novell","searchsubcontainer":true}, {"name":"admin","queryname":"Query_3","type":"user","context":"microfocus","searchsubcontainer":true}, {"name":"*","type":"","context":"novell","searchsubcontainer":true,"queryname":"default"}, {"name":"John","type":"user","context":"novell","searchsubcontainer":true,"auxClasses":[],"advancedFlag":true,"advancedFilter":{"condition":"and","rules":[{"field":"city","operator":"equals","value":"provo"},{"field":"personalTitle","operator":"equals","value":"employee"}]},"queryname":"john"}, {"name":"jack","type":"user","context":"novell","searchsubcontainer":true,"auxClasses":[],"advancedFlag":true,"advancedFilter":{"condition":"and","rules":[{"field":"city","operator":"equals","value":"utah"},{"field":"personalTitle","operator":"equals","value":"manager"}]},"queryname":"jack"}, {"name":"Martin","type":"user","context":"novell","searchsubcontainer":true,"queryname":"martin"}, {"name":"Luke","type":"user","context":"novell","searchsubcontainer":true,"queryname":"luke"}, {"name":"Anay","type":"user","context":"novell","searchsubcontainer":true,"queryname":"Anay"} ] 删除文件中的特定条目。有没有办法从json数组中读取对象?

1 个答案:

答案 0 :(得分:0)

您可以使用运算符delete从json中删除特定键。

&#13;
&#13;
const json = [{
    "name": "a",
    "queryname": "Query_1",
    "type": "user",
    "context": "novell",
    "searchsubcontainer": false
  },
  {
    "name": "aa",
    "queryname": "Query_2",
    "type": "user",
    "context": "novell",
    "searchsubcontainer": true
  },
  {
    "name": "admin",
    "queryname": "Query_3",
    "type": "user",
    "context": "microfocus",
    "searchsubcontainer": true
  },
  {
    "name": "*",
    "type": "",
    "context": "novell",
    "searchsubcontainer": true,
    "queryname": "default"
  },
  {
    "name": "John",
    "type": "user",
    "context": "novell",
    "searchsubcontainer": true,
    "auxClasses": [],
    "advancedFlag": true,
    "advancedFilter": {
      "condition": "and",
      "rules": [{
        "field": "city",
        "operator": "equals",
        "value": "provo"
      }, {
        "field": "personalTitle",
        "operator": "equals",
        "value": "employee"
      }]
    },
    "queryname": "john"
  },
  {
    "name": "jack",
    "type": "user",
    "context": "novell",
    "searchsubcontainer": true,
    "auxClasses": [],
    "advancedFlag": true,
    "advancedFilter": {
      "condition": "and",
      "rules": [{
        "field": "city",
        "operator": "equals",
        "value": "utah"
      }, {
        "field": "personalTitle",
        "operator": "equals",
        "value": "manager"
      }]
    },
    "queryname": "jack"
  },
  {
    "name": "Martin",
    "type": "user",
    "context": "novell",
    "searchsubcontainer": true,
    "queryname": "martin"
  },
  {
    "name": "Luke",
    "type": "user",
    "context": "novell",
    "searchsubcontainer": true,
    "queryname": "luke"
  },
  {
    "name": "Anay",
    "type": "user",
    "context": "novell",
    "searchsubcontainer": true,
    "queryname": "Anay"
  }
];

json.forEach((x) => {
  delete x.queryname;
});

console.log(json);
&#13;
&#13;
&#13;