2011-02-28 42 views
0

下面是一個示例字符串:成功解析格式不正確的JSON字符串嗎?

String s = "{\"source\": \"another \"quote inside\" text\"}"; 

什麼是解析這個最好的方法是什麼?我已經嘗試過4個解析器:JSON-lib的JSON-簡單GSON,並內置JSON解析器Grails的。

我使用Java和我想知道,如果有受涼MalformedJsonException或東西后把字符串的方式。

注:或者,這可能是Twitter的API的錯誤嗎?下面是一個示例響應字符串:

{ 
    "coordinates": null, 
    "user": { 
     "is_translator": false, 
     "show_all_inline_media": false, 
     "following": null, 
     "geo_enabled": false, 
     "profile_background_color": "C0DEED", 
     "listed_count": 11, 
     "profile_background_image_url": "http://a3.twimg.com/a/1298064126/images/themes/theme1/bg.png", 
     "favourites_count": 4, 
     "followers_count": 66, 
     "contributors_enabled": false, 
     "statuses_count": 1078, 
     "time_zone": "Tokyo", 
     "profile_text_color": "333333", 
     "friends_count": 51, 
     "profile_sidebar_fill_color": "DDEEF6", 
     "id_str": "107723125", 
     "profile_background_tile": false, 
     "created_at": "Sat Jan 23 14:16:03 +0000 2010", 
     "profile_image_url": "http://a3.twimg.com/profile_images/652140488/--------------_normal.jpg", 
     "description": "Mu8ecdu56e3u306eu56e3u9577u3068u30eau30fcu30c0u30fcu3067u3059u3002u8da3u5473u306fu7af6u99acu306eu4e88u60f3u3068u30b0u30e9u30c3u30d7u30eau30f3u30b0u3068u6253u6483u3092u30e1u30a4u30f3u3068u3057u3066u3044u307eu3059u3063uff01", 
     "location": "u5bccu5c71u770c", 
     "notifications": null, 
     "profile_link_color": "0084B4", 
     "protected": false, 
     "screen_name": "mattsun0209", 
     "follow_request_sent": null, 
     "lang": "ja", 
     "profile_sidebar_border_color": "C0DEED", 
     "name": "u307eu3063u3064u3093", 
     "verified": false, 
     "id": 107723125, 
     "profile_use_background_image": true, 
     "utc_offset": 32400, 
     "url": null 
    }, 
    "in_reply_to_screen_name": null, 
    "in_reply_to_status_id": null, 
    "in_reply_to_status_id_str": null, 
    "in_reply_to_user_id": null, 
    "text": "u3042u30fcu3001u7d50u819cu708eu306bu306au3063u3066u3057u307eu3063u305fu3002", 
    "contributors": null, 
    "retweeted": false, 
    "in_reply_to_user_id_str": null, 
    "retweet_count": 0, 
    "source": "u003Ca href="http: //twtr.jp" rel="nofollow"u003EKeitai Webu003C/au003E", 
    "id_str": "42128197566861312", 
    "created_at": "Mon Feb 28 07:45:19 +0000 2011", 
    "geo": null, 
    "entities": { 
     "hashtags": [], 
     "user_mentions": [], 
     "urls": [] 
    }, 
    "truncated": false, 
    "place": null, 
    "id": 42128197566861312, 
    "favorited": false 
} 

。注意到source屬性:

"source": "u003Ca href="http: //twtr.jp" rel="nofollow"u003EKeitai Webu003C/au003E" 
+0

你可以顯示你解析的代碼嗎? – 2011-02-28 12:52:19

+0

@Suresh String s =「{\」source \「:\」another \「quote in \」text \「}」; Gson gson = new Gson(); GsonVo obj2 = null; try {obj2 = gson.fromJson(s,GsonVo.class); } 趕上(JsonSyntaxException E){ logger.error(E,E); } logger.debug(obj2.text2); – firnnauriel 2011-02-28 13:11:40

+0

編輯您的問題並放置此代碼。 – 2011-02-28 13:15:02

回答

6

恐怕這是一個典型的「垃圾進,垃圾出」的局面。 JSON是無效,所以你不能正確解析它。你只能猜測它意味着什麼。現在,我們人類可以很好地猜測(明顯)是什麼意思,但在解析器級別上這更困難。

如果你知道一貫你得到這個無效source屬性,可以預先處理字符串反序列化之前,但真正的解決必須是在無效數據  — Twitter或任何蠢的來源(因爲它是)提供它。我假設這是您收到的實際字符串數據,而不是它的處理表單。

+0

嗯..所以這確實是Twitter API中的一個問題。希望他們能儘快修復。請注意,如果字符串替換爲「{\」source \「:\」另一個\ \ \「在\\」文本\「}中引用」「,那就沒問題。 – firnnauriel 2011-02-28 12:53:14

+0

我明白了。我想這是一個很好的解決方法。謝謝! – firnnauriel 2011-02-28 12:54:43

+0

@firnnauriel:我強烈建議告訴Twitter這個問題。他們無法解決他們不知道的事情。 :-) – 2011-02-28 12:55:23

0

預解析它之前處理數據。

對於每一行,找到第一個冒號(假設:在屬性名沒有冒號),然後逃逸每次上線雙引號除了冒號後的第一個和最後一個就行了。

+0

是的,這將是解決方案,如果格式是這樣的,但我得到一個'縮小'版本的整個JSON字符串。現在想着如何只替換源代碼部分.. – firnnauriel 2011-02-28 13:04:12

0

按照JSON grammar,格式是無效的。除非你在Twitter上工作,否則唯一可行的選擇是在解析之前對響應進行預處理。