javascript
  • json
  • parsing
  • 2015-10-07 132 views 1 likes 
    1

    我得到一個錯誤,當我解析JavaScript中的json字符串。 「未捕獲的SyntaxError:意外的令牌Z」我知道它的「源」標記中的雙重qoutes,但我該如何解析?javascript json解析錯誤Doubleqoutes

    <script type="text/javascript"> 
        var data = '[ { "Outcome": "Success", "Message": null, "Identity": "Request", "Delay": 0.115825, "BaseCurrency": "EUR", "QuoteCurrency": "USD", "Symbol": "EURUSD", "Date": "10/07/2015", "Time": "2:46:32 PM", "QuoteType": "Spot", "Bid": 1.12394, "Mid": 1.12397, "Ask": 1.124, "Spread": 0.00006, "Text": "1 European Union euro = 1.12397 United States dollars", "Source": "SIX Financial Information, Buyer = \"ZKB Zuercher Kantonalbank, Devisen\", Seller = \"ZKB Zuercher Kantonalbank, Devisen\"" }, { "Outcome": "Success", "Message": null, "Identity": null, "Delay": 0, "BaseCurrency": "GBP", "QuoteCurrency": "USD", "Symbol": "GBPUSD", "Date": "10/07/2015", "Time": "2:46:32 PM", "QuoteType": "Spot", "Bid": 1.5293, "Mid": 1.5295, "Ask": 1.5297, "Spread": 0.0004, "Text": "1 British pound sterling = 1.5295 United States dollars", "Source": "SIX Financial Information, Buyer = \"UBS Investment Bank\", Seller = \"UBS Investment Bank\"" } ]'; 
        var obj = JSON.parse(data); 
        for(var key in obj){ 
         console.log(obj[key].Symbol);  
        } 
    </script> 
    

    我可以使用替換方法,但那不是一個乾淨的解決方案,我認爲。

    +0

    使用'\「'引用字符串內,你在'Source'是法律有見json.org –

    +0

    ''\''是字符串字面特殊字符*和*。 JSON。在字符串文字中使用JSON並不常見,爲什麼在這裏首先使用JSON? –

    回答

    2

    不知道你爲什麼在這裏使用JSON。只是這樣做:

    <script type="text/javascript"> 
        var obj = [ { "Outcome": "Success", "Message": null, "Identity": "Request", "Delay": 0.115825, "BaseCurrency": "EUR", "QuoteCurrency": "USD", "Symbol": "EURUSD", "Date": "10/07/2015", "Time": "2:46:32 PM", "QuoteType": "Spot", "Bid": 1.12394, "Mid": 1.12397, "Ask": 1.124, "Spread": 0.00006, "Text": "1 European Union euro = 1.12397 United States dollars", "Source": "SIX Financial Information, Buyer = \"ZKB Zuercher Kantonalbank, Devisen\", Seller = \"ZKB Zuercher Kantonalbank, Devisen\"" }, { "Outcome": "Success", "Message": null, "Identity": null, "Delay": 0, "BaseCurrency": "GBP", "QuoteCurrency": "USD", "Symbol": "GBPUSD", "Date": "10/07/2015", "Time": "2:46:32 PM", "QuoteType": "Spot", "Bid": 1.5293, "Mid": 1.5295, "Ask": 1.5297, "Spread": 0.0004, "Text": "1 British pound sterling = 1.5295 United States dollars", "Source": "SIX Financial Information, Buyer = \"UBS Investment Bank\", Seller = \"UBS Investment Bank\"" } ]; 
        for(var key in obj){ 
         console.log(obj[key].Symbol);  
        } 
    </script> 
    
    +0

    我是Json的新成員,但是謝謝我從來沒有解析過它,它的工作原理如下: – Vazzilly

    +0

    @Vazzilly,請注意在我的答案中沒有JSON,只有一個對象字面值,JSON(它總是一個字符串)和一個對象字面值之間有區別 –

    +0

    好吧,謝謝你瞭解到,因爲Data API調用它Json ive叫它Json藏漢 – Vazzilly

    3

    您還需要避開反斜槓。代替\",請鍵入\\"

    相關問題