2014-04-04 190 views
0

我正在處理其中發送請求ot服務器獲取JSON數組字符串的項目。我正在使用下面的java代碼來創建一個JSON數組列表字符串。無法解析JSON數組字符串

[{"duration":12222‚"height":12‚"widht":12‚"filePath":30‚"format":123‚"bitRate":1111‚"exe":"m"}‚{"duration":12222‚"height":12‚"widht":12‚"filePath":30‚"format":123‚"bitRate":1111‚"exe":"m"}‚{"duration":12222‚"height":12‚"widht":12‚"filePath":30‚"format":123‚"bitRate":1111‚"exe":"m"}‚{"duration":12222‚"height":12‚"widht":12‚"filePath":30‚"format":123‚"bitRate":1111‚"exe":"m"}] 

當我使用JQuery如下解析它:

JSONArray itemList = new JSONArray(); 
for(int i =0; i<4; i++) { 
    String exe = "m"; 
    final JSONObject jsonObj = new JSONObject(); 
    jsonObj.put("filePath", 30); 
    jsonObj.put("duration", 12222); 
    jsonObj.put("bitRate", 1111); 
    jsonObj.put("widht", 12); 
    jsonObj.put("format", 123); 
    jsonObj.put("height", 12); 
    jsonObj.put("exe", exe); 
    JSONObject jObject = new JSONObject(); 

    try { 
     itemList.put(jsonObj); 
     // jObject.put("itemList", itemList); 
    } catch (Exception e) { 
     System.out.println("ERROR"); 
    } 
} 
return itemList.toString(); 

在客戶機側,在AJAX響應,我通過使用上述方法得到下面的字符串

$jQ.each($jQ.parseJSON(responseJSON), function(idx, obj) { 
    alert(obj.filePath); 
}); 

我得到JS錯誤爲JSON.parse: expected property name or '}'

我不明白爲什麼這個錯誤是occ uring。

+1

在契約中有你的JSON錯誤,粘貼JSON這裏 - > http://jsonlint.com/ 你會發現爲什麼 – Defoncesko

+0

你得到JSON是JavaScript的OBJ ...你沒必要解析它我認爲 – VostanAzatyan

+0

您的JSON無效...... – VostanAzatyan

回答

1

根據jsonlint.com,你應該把值放入""

由於特殊字符&

1

只要看看前幾行:

[{"duration":12222&sbquo;"height":12&sbquo;

這似乎並非是有效的JSON。 duration是關鍵,值是12222這是Integer,但是你也有字符串數據&sbquo;旁邊的int,這使得這個無效的json數據。

如果您有混合數據,請用雙引號將其封裝爲字符串。


更新

,是HTML編碼爲&sbquo; - 有你的問題。

試試這個JSON字符串:

[ 
    { 
     "duration": 12222, 
     "height": 12, 
     "widht": 12, 
     "filePath": 30, 
     "format": 123, 
     "bitRate": 1111, 
     "exe": "m" 
    }, 
    { 
     "duration": 12222, 
     "height": 12, 
     "widht": 12, 
     "filePath": 30, 
     "format": 123, 
     "bitRate": 1111, 
     "exe": "m" 
    }, 
    { 
     "duration": 12222, 
     "height": 12, 
     "widht": 12, 
     "filePath": 30, 
     "format": 123, 
     "bitRate": 1111, 
     "exe": "m" 
    }, 
    { 
     "duration": 12222, 
     "height": 12, 
     "widht": 12, 
     "filePath": 30, 
     "format": 123, 
     "bitRate": 1111, 
     "exe": "m" 
    } 
] 

現在這個驗證。

+0

您是否有該html編碼的來源?在線工具,我發現不HTML編碼逗號‚,如果他們確實把它包括它應該是, –

+0

http://www.w3.org/wiki/Common_HTML_entities_used_for_typography(單低價格) - 也許他錯了錯誤的逗號? – Latheesan

+0

也許這是取代它... –

0

您的Java環境可能會錯誤地序列化數組。

而不是‚ - 單底部報價U + 201A - 你應該有一個逗號(,U + 002C)。