2016-01-06 45 views
0

在下面的代碼,JSON不能很好形成錯誤

<head> 
     <meta charset="UTF-8"> 
     <title>JSON ex</title> 
     <script type = "text/javascript" 
      src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> 
     </script> 

     <script type="text/javascript" language = "javascript"> 
      var accountGrid = []; 
      $(document).ready(function(){ 
       $.getJSON('result.json', function(entry){ 
        accountGrid.push({ 
         name: entry.name, 
         marketValue: entry.marketValue, 
         cash: entry.cash, 
         legend: entry.legend 
        }); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 

    </body> 

result.json

{ 
    'name': 'Account1', 
    'marketValue': '10990', 
    'cash': '199926', 
    'legend': 'orange' 
}, 
{ 
    'name': 'Account2', 
    'marketValue': '156590', 
    'cash': '133856', 
    'legend': 'darkorange' 
} 

我看到JSON not well formed錯誤。 result.json坐在同一個文件夾中。

由於此錯誤,accountGrid變空。

在執行jQuery.getJSON時出錯not well formed是什麼意思?

+2

您可以使用在線/脫機驗證程序來驗證您的JSON。 – Drumbeg

回答

6

讓我們考慮grammar of JSON ...

JSON必須是單個值(對象,數組等)

的JSON對象應該是一個單一的值。你有兩個用逗號分開的對象。也許你可以將它們分配爲單個對象的成員。

# a JSON object consists of a single value definition 
value 
    string 
    number 
    object 
    array 
    true 
    false 
    null 

# this value can be an object 
object 
    {} 
    { members } 

# or an array 
array 
    [] 
    [ elements ] 

______

單引號是非法

JSON禁止使用單引號(')的字符串。

# a string consists of chars wrapped in double-quotation marks 
string 
    "" 
    " chars " 

因此,將所有單引號替換爲雙引號。

考慮上述兩種東西,你將結束與這樣的:

{ 
    "key1": { 
     "name": "Brokerage Account 3", 
     "marketValue": "1999990", 
     "cash": "1995826", 
     "legend": "orange" 
    }, 
    "key2": { 
     "name": "Account 3", 
     "marketValue": "1949990", 
     "cash": "1695856", 
     "legend": "darkorange" 
    } 
} 

[Try it out online]

______

放置在陣列內的對象

替代地,正如@Gerald Schneider所建議的那樣,將對象放置在一個數組中。該規範指出,value(上面定義的)可以是array

array 
    [] 
    [ elements ] # elements can multiple values, e.g. objects 

所以,你的JSON應該是這樣的:

[ 
    { 
     "name": "Account1", 
     "marketValue": "10990", 
     "cash": "199926", 
     "legend": "orange" 
    }, 
    { 
     "name": "Account2", 
     "marketValue": "156590", 
     "cash": "133856", 
     "legend": "darkorange" 
    } 
] 

[Try it out online]

______

消費解析的JSON(其中JSON是一個數組)

如果你代表你的數據作爲一個數組,你的回調應該簡單地分配所產生的解析的JSON給accountGrid變量:

<script type="text/javascript" language = "javascript"> 
    var accountGrid = []; 
    $(document).ready(function(){ 
     $.getJSON('result.json', function(entry){ 
      accountGrid = entry; 
     }); 
    }); 
</script> 

另外,如果你想進入的價值附加到accountGrid

accountGrid = accountGrid.concat(entry); 

______

寫作JSON在未來

我建議您在支持JSON語法高亮的IDE中編輯JSON文件,然後在編輯器中引發這些問題。或者,use one of the many online tools available

+2

就我個人而言,我只是把它放在'[]'中而已。使循環更容易。 –

+0

我使用數組語法在行1:1處得到語法錯誤 – overexchange

+0

[在此嘗試](http://www.jsoneditoronline.org/?id=572498b110113540bd833c1e3de7fed0)。 – sdgluck

相關問題