以下是我从 CouchDB 数据库中检索到的原始 JSON 对象:
{
"total_rows": 3,
"offset": 0,
"rows": [
{
"id": "1e8fe199d4e761b71bb8920eb3003db2",
"key": null,
"value": {
"_id": "1e8fe199d4e761b71bb8920eb3003db2",
"_rev": "3-6979e0cd646f2bc0ddf84c0664d25808",
"Room_number": 99,
"Location": "City campus",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
},
{
"id": "1e8fe199d4e761b71bb8920eb3004cc6",
"key": null,
"value": {
"_id": "1e8fe199d4e761b71bb8920eb3004cc6",
"_rev": "2-f7250cca62a804137174a20f48312c40",
"Room_number": 12,
"Location": "City hall",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
]
}
我想让它看起来像下面的JSON对象:
[
{
"Room_number": 99,
"Location": "City campus",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
},
{
"Room_number": 12,
"Location": "City hall",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
]
这是我到目前为止所得到的:
[
{
"value": {
"Room_number": 99,
"Location": "City campus",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
},
{
"value": {
"Room_number": 12,
"Location": "City hall",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
}
]
这是我用来实现此目的的 NodeJS 代码:
var express = require('express')
, db = require('nano')('http://localhost:5984/rooms_database')
, app = express();
// query a view
db.view('show_rooms', 'view_show_rooms', function (err, doc)
{
app.get("/", function(request,response)
{
var json_data = doc;
json_data = json_data.rows;
for (var i = 0; i < json_data.length; i++)
{
delete json_data[i].id;
delete json_data[i].key;
delete json_data[i].value._id;
delete json_data[i].value._rev;
}
response.send(json_data);
});
});
app.listen(8080);
console.log('************ Server is running! ************');
我在 CouchDB :
中使用标准视图function(doc)
{
emit(null, doc);
}
有谁能告诉我如何摆脱我的JSON对象中的'value'
键?
答案 0 :(得分:6)
您可以使用Array.map()
映射这样的每个项目,保留您想要的内容,而不是删除您不想要的内容:
var output = json_data.rows.map(function(item) {
return {
Room_number: item.value.Room_number,
Location: item.value.Location,
Timetable: item.value.Timetable
}
});
答案 1 :(得分:3)
我认为这样做会:
response.send (json_data.map (function (v) { return v.value; }));
答案 2 :(得分:2)
你可以这样做
var json_data = doc;
json_data = json_data.rows;
var output=[];
for (var i = 0; i < json_data.length; i++)
{
var temp = json_data[i].value;
delete temp._id;
delete temp._rev;
output.push(temp);
}
response.send(output);