2017-02-04 18 views
0

Может ли кто-нибудь дать мне понять, что не так с синтаксисом 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" 
} 
+0

Вы не можете иметь разрывы строк в строковых значений , – JJJ

+0

Возможный дубликат [Недопустимая ошибка Json в JsonLint] (http://stackoverflow.com/questions/21675038/invalid-json-error-in-jsonlint) – JJJ

+0

@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

ответ

-2

Здесь исправлен без мультилиний:

{ 
    "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);", 
     "}", 
     "}", 
     "" 
    ] 
    } 
} 
+0

Почему downvoted, я думаю, это единственный * читаемый * способ многострочного json, здесь: http://stackoverflow.com/questions/2392766/multiline-strings-in-json он также используется. – BladeMight

+2

Разве эта модификация полностью не меняет смысл json? – jlb333333

+0

@ jlb333333 может быть так, но в JSON нет многострочной линии ... Но если вы читаете ответы/комментарии в теме, которую я предлагал, вы можете понять, почему я это предложил. – BladeMight

 Смежные вопросы

  • Нет связанных вопросов^_^