2012-02-22 164 views
1

這是我的代碼:JSONException:未結束的字符串

JSONObject msgObject = JSONObject.fromObject(msg); 

然後我得到一個JSONException:

net.sf.json.JSONException: Unterminated string at character 239 of {...} 

我在做什麼錯?

謝謝。

編輯:
我創建一個test.txt文件:

{"source":"a\n"}

的測試方法是這樣的:

Scanner cin = new Scanner(new File("test1.txt"), "utf-8");
JSONObject msgObject = JSONObject.fromObject(msg);

我得到這個:
鍵:源&值:一個

+0

的可能重複的[org.json.JSONException:在737 \ [字符738線1 \]未終止的字符串](http://stackoverflow.com/questions/4853917/org-json-jsonexception-unterminated-string-at -737-character-738-line-1) – dacwe 2012-02-22 09:18:55

+0

@dacwe它不起作用): – Felix 2012-02-22 10:19:15

回答

0

看來您的msg內容不正確,您是否嘗試過驗證您的JSON?你可以試試這裏:http://jsonlint.com/

編輯:對不起,我推測你使用了JSON字符串。以下是API的報價:

Creates a JSONObject. 
Inspects the object type to call the correct JSONObject factory method. Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans. 

您的msg對象是否兼容?

+0

結果 有效的JSON – Felix 2012-02-22 09:21:52

+0

你確定沒有'\ n'或'\ r'可以「剪切」一個字符串你的JSON? – 2012-02-22 09:27:33

+0

有一個'\ n'。但它在http://jsonlint.com/上運行良好。我認爲這個問題現在在這裏:(@Michael Laffargue Easysport.fr – Felix 2012-02-22 09:34:06