2011-09-25 14 views
0

我使用以下CouchDB設計文檔進行測試。我使用回車符\ n和製表符\ t來縮進我的Javascript代碼。據我瞭解JSON,我認爲這些地方的空白是好的。但是Ruby couchrest也無法解析這個和JSLint。在解析它之前,我必須從設計文檔中去除\ n和\ t。這是爲什麼 ?Jouch解析CouchDB設計文檔時出錯:由於縮進空格而導致未關閉的字符串?

上json.org的JSON介紹頁提到,「空白可以在任何一令牌之間插入。」

這是一個有點難以這裏呈現的標籤,所以我已經包括瞭解析字符串\ n和T。

{ 
"_id": "_design/test", 
"views": { 
    "all": { 
     "map": "function(doc) { 
      if (doc.nom) emit(doc._id, doc); 
      }" 
     }, 
    "asin_doc": { 
     "map": "function(doc) { 
      if (doc.category) emit(doc.asin, doc); 
      }" 
     }, 
    "asin": { 
     "map": "function(doc) { 
      if (doc.category) emit(doc.asin, null); 
      }" 
     } 
    } 
    } 

在一個長字符串(紅寶石)的同一個文件:

"{\n\t\"_id\": \"_design/test\",\n\t\"views\": {\n\t\t\"all\": {\n\t\t\t\"map\": \"function(doc) {\n\t\t\t\tif (doc.nom) emit(doc._id, doc);\n\t\t\t}\"\n\t\t},\n\t\t\"asin_doc\": {\n\t\t\t\"map\": \"function(doc) {\n\t\t\t\tif (doc.category) emit(doc.asin, doc);\n\t\t\t}\"\n\t\t},\n\t\t\"asin\": {\n\t\t\t\"map\": \"function(doc) {\n\t\t\t\tif (doc.category) emit(doc.asin, null);\n\t\t\t}\"\n\t\t}\n\t}\n}\n" 

JSLint的告訴我,有5行字的問題20

Problem at line 5 character 20: Unclosed string. 
"map": "function(doc) { 
Problem at line 5 character 20: Stopping. (25% scanned). 
JSON: bad. 

我不能看看字符串沒有正確關閉的地方。任何想法 ?

回答

1

字符串中不允許換行符和製表符。見JSON RFC,具體是:

unescaped = %x20-21/%x23-5B/%x5D-10FFFF 

編輯:什麼json.org在談論,我想是這樣的:

不重要的空白之前或之後任何六個結構特徵的允許。

的 「結構特徵」 是:[ ] { } : ,

相關問題