如何从基于JSON的数据中删除带有ID的信息

时间:2016-08-08 13:54:36

标签: json node.js

我的JSON文件的内容是: 这是我的retails.json文件。实际零售是我的database.i从sql database.so转换这个json文件在这个json文件中,我想用一个ID删除一个人的信息。我该怎么办?我正在使用node.js和express。

{
"categories" : [
{
"dept_id" : "123",
"category_name" : "database",
"category_discription" : "list of database",
"current time" : "2016-07-21 06:27:17",
"id" : "1"
},
{
"dept_id" : "1234",
"category_name" : "debugging",
"category_discription" : "program debugger",
"current time" : "2016-07-21 06:32:24",
"id": "2"
},
{
"dept_id" : "12345",
"category_name" : "system analyzer",
"category_discription" : null,
"current time" : "2016-07-21 06:33:23",
"id" : "3"
}
],
"departments" : [
{
"name" : "manpreet singh",
"address_info" : "qadian",
"current time" : null,
"id" : "1"
},
{
"name" : "tushal gupta",
"address_info" : "amritsar",
"current time" : "2016-07-21 06:10:14",
"id" : "2"
},
{
"name" : "haroop singh",
"address_info" : "amritsar",
"current time" : "2016-07-21 06:11:12",
"id" : "3"
}
],

"digital_marketing" : [
{
"dept_id" : "123",
"phone" : "99889988",
"mobile" : null,
"email" : "thbs@gmail.com",
"web" : null,
"facebook" : null,
"twitter" : null,
"linkedin" : null,
"current time" : "2016-07-21 06:10:16",
"id" : "1"
},
{
"dept_id" : "1234",
"phone" : "998899888",
"mobile" : null,
"email" : null,
"web" : null,
"facebook" : "gtudgal@fb.com",
"twitter" : "tushalgupta",
"linkedin" : null,
"current time" : "2016-07-21 06:30:19",
"id" : "2"
},
{
"dept_id" : "12345",
"phone" : "99889877",
"mobile" : null,
"email" : "fhdts@mail.com",
"web" : null,
"facebook":"sdfh33@fb.com",
"twitter" : null,
"linkedin" : null,
"current time" : "2016-07-21 06:30:13",
"id" : "3"  
} 
]
}

我用它来删除ID,但不起作用:

var id = 2;
app.get('/deleteUser', function (req, res) {   
// First read existing users.  
fs.readFile( __dirname + "/" + "retails.json", 'utf8', function (err, data) {       
data = JSON.parse( data );      
 delete data["categories" + 2];       
     console.log( data );
     res.end( JSON.stringify(data));
   });
});

1 个答案:

答案 0 :(得分:0)

for (var prop in jsonObj.categories){
   delete jsonObj.categories[prop].id;
}

类似于digital_marketing等.jsonObj是您案例中的数据..