我有一个节点控制台应用程序,我试图将一些记录发布到本地CouchDB实例上的批量文档api并获得“无效的UTF-8 JSON”。是什么让这个特别奇怪,就是我在对象文字上使用JSON.stringify生成我的JSON。
我在http://jsonlint.com/测试了实际的json,据说它是有效的。我不能在这里发布完整的json,因为它是一个目录中的名字和编号,但我可以向你展示一个模拟记录,为你提供基本的结构。
{
"family" : "Smith",
"people":{
"Bob":{
"name":"Bob",
"active" : true,
"birthday" : "1/01"
},
"Sue":{
"name": "Sue",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1111 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4908"
};
我将我的记录数组包装在一个json对象中,其属性为“docs”,指定为here。我正在做的事情有什么明显的错误吗?
更新: 按照lambmj的建议,我将节点生成的字符串写入文件,然后使用curl将其发布到批量文档上传。这非常有效。我仍然希望有人帮助我纠正我的节点代码,以便它可以在节点内工作。以下是构建和发布请求的确切代码
//The result variable is my array of 'family' objects,
//It's generated by parsing a text file, not show here
var postOptions = {
host: 'localhost',
port: '5984',
path: '/members/_bulk_docs',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': result.length
}
};
var request = http.request(postOptions, function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
var body = { "docs": result};
var stringData = JSON.stringify(body);
console.log(stringData);
request.write(stringData);
答案 0 :(得分:4)
我能够使用以下命令让您的示例正常工作:
curl -X POST -H "Content-type:application/json" \
http://localhost:5984/test/_bulk_docs -d @family.json
其中family.json
包含以下内容。我添加了第二个系列,以便阵列中有多个元素。
{"docs": [
{"family" : "Smith",
"people":{
"Bob":{
"name":"Bob",
"active" : true,
"birthday" : "1/01"
},
"Sue":{
"name": "Sue",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1111 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4908"
},
{"family" : "Jones",
"people":{
"John":{
"name":"John",
"active" : true,
"birthday" : "1/01"
},
"Mary":{
"name": "Mary",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1112 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4909"
}
]}
JSON与包含在数组中的提供相同,并作为传递给CouchDB的JSON的docs
属性的值提供。您给出的示例中还有一个尾部分号(;
)。如果其他所有格式都正确,那可能就是问题。
更新:
好的,这是答案节点代码。我按代码中的说明进行了3次更改:
#!/usr/bin/env node
var http = require("http");
// Embedding the family object here so that this test program will compile/run.
result = {
"family" : "Smith",
"people":{
"Bob":{
"name":"Bob",
"active" : true,
"birthday" : "1/01"
},
"Sue":{
"name": "Sue",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1111 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4908"
};
var body = { "docs": [result]}; // Change #1: docs takes an array.
var stringData = JSON.stringify(body); // Change #2: stringify first.
var postOptions = {
host: 'localhost',
port: '5984',
path: '/members/_bulk_docs',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': stringData.length // Change #3: send the length of the stringified data.
}
};
var request = http.request(postOptions, function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
console.log(stringData);
request.write(stringData);
request.end();
这是我从节点获得的输出:
{"docs":[{"family":"Smith","people":{"Bob":{"name":"Bob","active":true,"birthday":"1/01"},"Sue":{"name":"Sue","active":true,"birthday":"1/01"}},"address":{"street":"1111 Cool Road","city":"Cincinnati","state":"OH","zip":"11111"},"phone":"923-4908"}]}
Response: [{"ok":true,"id":"721b8cde7a1e22b4cc106adbb3f41df9","rev":"1-306b71ff83df48c588a174fd5feafa34"}]