2014-09-19 49 views
-1
{ 
    "id": 1001, 
    "text": "Core Java - Collections", 
    "description": "public static void main(String args[ ]) { \n int x =10;\n}", 
    "passingScore": 10, 
    "totalQuestions": 20, 
    "technologyId": 10, 
    "subTechnologyId": 101 
}{ // fails on this line 
    "id": 1000, 
    "text": "Core Java", 
    "description": "public static void main(String args[ ]) { \n int x =10;\n}", 
    "passingScore": 10, 
    "totalQuestions": 5, 
    "technologyId": 10, 
    "subTechnologyId": 100 
} 

我沒有看到上述JSON的任何錯誤,但不知何故失敗。任何人都可以幫我找出錯誤嗎?以下JSON有什麼問題?

+0

是什麼讓你認爲「失敗」? (天哪 - 網上1分鐘:你錯過了逗號。) – usr2564301 2014-09-19 21:45:18

+3

兩個「對象」之間沒有逗號,也沒有周圍的「數組」。去json.org學習JSON語法 - 它只需要5-10分鐘。 – 2014-09-19 21:47:08

+0

你可以嘗試驗證它在這裏:http://jsonlint.com/ – melancia 2014-09-19 21:47:41

回答

3

的主要問題是,你只能有一個頂級項,它必須是一個對象({})或陣列([])。第二個問題是如果你有多個項目,你必須用逗號分隔它們。

例如,這是有效的:

[ 
    { 
     "id": 1001, 
     "text": "Core Java - Collections", 
     "description": "public static void main(String args[ ]) { \n int x =10;\n}", 
     "passingScore": 10, 
     "totalQuestions": 20, 
     "technologyId": 10, 
     "subTechnologyId": 101 
    }, 
    { 
     "id": 1000, 
     "text": "Core Java", 
     "description": "public static void main(String args[ ]) { \n int x =10;\n}", 
     "passingScore": 10, 
     "totalQuestions": 5, 
     "technologyId": 10, 
     "subTechnologyId": 100 
    } 
] 

在那裏,我已經添加[]使其成爲一個數組,對象之間用逗號。

或者,如果它不應該是一個數組,你可以把一個對象有兩個鍵:

{ 
    "first": { 
     "id": 1001, 
     "text": "Core Java - Collections", 
     "description": "public static void main(String args[ ]) { \n int x =10;\n}", 
     "passingScore": 10, 
     "totalQuestions": 20, 
     "technologyId": 10, 
     "subTechnologyId": 101 
    }, 
    "second": { 
     "id": 1000, 
     "text": "Core Java", 
     "description": "public static void main(String args[ ]) { \n int x =10;\n}", 
     "passingScore": 10, 
     "totalQuestions": 5, 
     "technologyId": 10, 
     "subTechnologyId": 100 
    } 
} 
0

這不是JSON。 JSON文檔是數組或字典。你有兩本字典。喜歡 { } { }。您需要一個由逗號分隔的字典數組。喜歡 [ { }, { } ]。

解決方法:告訴誰提供了假定的「JSON」來修復它。另外,你可以解析任何這是手動。實際上,您所需要的只是找到匹配的大括號,將數據拆分成小塊並分別進行解析。十分難看;我拒絕這樣做。