2011-02-28 108 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上工作,否则唯一可行的选择是在解析之前对响应进行预处理。