2017-02-04 16 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

[JsonLintの無効なJsonエラー]の可能な複製(http://stackoverflow.com/questions/21675038/invalid-json-error-in-jsonlint) – JJJ

+0

@JJJコメントありがとうございます。全体的な文書を含め、縮小されたjsonの編集を追加しました。私はダブルチェックとしてhttp://codebeautify.org/jsonvalidatorを使用しました。 JsonLintとCodeBeautifyは両方ともこの地域を誤って指摘しています: 'line + =' "'+ val.replace(/"、 "" "')+ ''コードはCloudantウェブサイトに掲載されています。 。 [link](https://developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-orical/) – 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