簡體   English   中英

意外的令牌ILLEGAL:如何刪除“ e;”和JSON中的換行符

[英]Unexpected token ILLEGAL : how to remove &quote; and lines breaks from JSON

加載頁面時,我從服務器接收到 ,並使用Handelbars.java將其填充到頁面中。 到現在為止一切都很好。

但是,我想獲取該對象並將其存儲在對象上,這是我失敗的地方。

當我嘗試將其存儲在對象上時,請執行以下操作:

var jsObject = "{{output.items}}";  // the output.items is the JSON retrieved on the template

我得到以下內容( &quote;和換行符解釋):

{
    "profile": {
        "name": "copernic",
        "email": "copernic@cop.com"
    }
    ....
}

當我應該得到以下內容時(不解釋換行符):

{
    "profile": {
        "name": "copernic",
        "email": "copernic@cop.com"
    }
    ....
}

因此,它在 Uncaught SyntaxError: Unexpected token ILLEGAL上引發了錯誤Uncaught SyntaxError: Unexpected token ILLEGAL

使用<pre>{{output}}</pre>在HTML模板上打印json時,它看起來還不錯。

您是否知道如何在頁面加載時將服務器返回的存儲在對象上,因為我沒有太多控制權,因為它不是 附帶的?

謝謝。

您的服務器提供的json格式不正確。

您的服務器需要提供帶雙引號的json字符串。

{
    "profile": {
        "name": "copernic",
        "email": "copernic@cop.com"
    }
}

如果您需要提供格式良好的json,可以嘗試使用Gson。 https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson

由於內容output.items包含字符串,因此可以繼續進行操作。

eval("jsObject={" + object.items.replace(/&quot;/g,'"')+"}");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM