JSON结构/语法

时间:2017-02-04 08:18:25

标签: json syntax structure

任何人都可以告诉我下面列出的JSON语法有什么问题。根据JsonLint,错误是/在单词{ "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); } } " } } 前面开始。我无法在语法中看到错误,但必须在那里。如果有人能告诉我我的错误背后的原则,请。

{
"_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"
}

编辑:

完整代码(CouchDB视图/列表):

readValue

1 个答案:

答案 0 :(得分:-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);",
      "}",
      "}",
      ""
    ]
  }
}