Может ли кто-нибудь дать мне понять, что не так с синтаксисом JSON, указанным ниже. Согласно JsonLint, ошибка начинается/начинается перед словом "csv"
. Я не вижу ошибки в синтаксисе, но он должен быть там. Если бы кто-то мог просто сказать мне принцип моей ошибки, пожалуйста.Структура JSON/Синтаксис
{
"lists": {
"csv": "function(head, req) {
var row,
first = true;
// output HTTP headers
start({
headers: {
'Content-Type': 'text/csv'
},
});
// iterate through the result set
while (row = getRow()) {
// get the doc (include_docs=true)
var doc = row.doc;
// if this is the first row
if (first) {
// output column headers
send(Object.keys(doc).join(',') + 'n');
first = false;
}
// build up a line of output
var line = '';
// iterate through each row
for (var i in doc) {
// comma separator
if (line.length > 0) {
line += ',';
}
// output the value, ensuring values that themselves
// contain commas are enclosed in double quotes
var val = doc[i];
if (typeof val == 'string' && val.indexOf(',') > -1) {
line += '" ' + val.replace(/"/g, ' "" ') + ' "';
} else {
line += val;
}
}
line += 'n';
// send the line
send(line);
}
}
"
}
}
EDIT:
Полный код (CouchDB вид/список):
{
"_id": "_design/comptno",
"_rev": "2-4531ba9fd5bcd6b7fbc5bc8555f0bfe3",
"views": {
"list_example": {
"map": "function(doc) {\r\n if (doc.compartment.number) {\r\n emit(doc.compartment.number, null);\r\n }\r\n};"
},
"list_example2": {
"map": "function(doc) {\r\n if (doc.compartment.number) {\r\n emit(doc.compartment.number, null);\r\n }\r\n};"
}
},
"lists":{"csv":"function(head, req) { var row, first = true; // output HTTP headers start({ headers: { 'Content-Type': 'text/csv' }, }); // iterate through the result set while (row = getRow()) { // get the doc (include_docs=true) var doc = row.doc; // if this is the first row if (first) { // output column headers send(Object.keys(doc).join(',') + 'n'); first = false; } // build up a line of output var line = ''; // iterate through each row for (var i in doc) { // comma separator if (line.length > 0) { line += ','; } // output the value, ensuring values that themselves // contain commas are enclosed in double quotes var val = doc[i]; if (typeof val == 'string' && val.indexOf(',') > -1) { line += '"' + val.replace(/"/g, '""') + '"'; } else { line += val; } } line += 'n'; // send the line send(line); }}"},
"language": "javascript"
}
Вы не можете иметь разрывы строк в строковых значений , – JJJ
Возможный дубликат [Недопустимая ошибка Json в JsonLint] (http://stackoverflow.com/questions/21675038/invalid-json-error-in-jsonlint) – JJJ
@JJJ Спасибо за комментарии. Я добавил редактирование с мини-json, включая весь документ для обзора. Я использовал http://codebeautify.org/jsonvalidator как двойную проверку. JsonLint и CodeBeautify указывают на этот регион как ошибочный: 'line + =' "'+ val.replace (/"/g,' "" ') +' 'Код был опубликован на веб-сайте Cloudant, и я прокомментировал его , [link] (https://developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/) – jlb333333