如何对 json 进行转义?

孙兴斌*_*孙兴斌 2 java string json escaping jackson

我有一个转义的 json 字符串,例如

\n\n
{\\"uid\\":\\"93\\",\\"channel_origin\\":\\"\\",\\"sys_ver\\":\\"12.2\\",\\"carrier_network_code\\":\\"02\\",\\"rpn\\":\\"\\\\u9996\\\\u9875\\",\\"app_ver\\":\\"9.3.8\\",\\"event_time\\":1556119062,\\"carrier\\":\\"\\\\u4e2d\\\\u56fd\\\\u79fb\\\\u52a8\\",\\"event_guid\\":\\"E36FE60BFBFF405680906E3F95EC3953\\",\\"ppn\\":\\"\\\\u542f\\\\u52a8\\",\\"device_token\\":\\"d323828f196a3ca665fa296a2a5f23a57cce83963328b73bd2cf78a7d55575ef\\",\\"day_first\\":\\"442\\",\\"abtest\\":\\"{\\\\\\"app_home_change\\\\\\":\\\\\\"b\\\\\\",\\\\\\"app_icon_change\\\\\\":\\\\\\"b\\\\\\"}\\",\\"pn\\":\\"\\\\u9996\\\\u9875\\",\\"puri\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/launch\\",\\"day_before\\":\\"1\\",\\"hardware_model\\":\\"iPhone10,3\\",\\"lng\\":\\"114.427362\\",\\"launch_guid\\":\\"69904D0912C943AC9D924AC8469CEA4E\\",\\"app_code\\":\\"cn.mafengwo.www\\",\\"_launch_source_history\\":\\"[]\\",\\"idfv\\":\\"20587F0C-0F6E-4CD2-94C3-CE3A10CF0EFD\\",\\"open_udid\\":\\"72F69B19-E612-4A4B-9D2F-8BB6C890421B\\",\\"carrier_country_code\\":\\"cn\\",\\"dev_ver\\":\\"D1908.0\\",\\"device_type\\":\\"ios\\",\\"ref\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/index?banner=1&channel_id=55&theme=1\\",\\"lat\\":\\"27.818787\\",\\"event_code\\":\\"show_index\\",\\"idfa\\":\\"DC7D35C1-C81B-4884-96C5-C6BAE83FA7DA\\",\\"uri\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/index?banner=1&channel_id=55&theme=1\\",\\"channel_code\\":\\"\\",\\"net\\":\\"1\\",\\"_is_foreground\\":\\"1\\",\\"sdk_ver\\":\\"20161011\\",\\"_event_hash\\":\\"8ae3b940026375b8252e689113fc8c43f5bfddcf\\",\\"ip\\":\\"39.176.24.64\\",\\"umddid\\":14696,\\"topic\\":\\"mobile_event\\",\\"ctime\\":1556119062,\\"ip_s\\":\\"node001026\\",\\"datetime\\":\\"2019-04-24T23:17:42+08:00\\",\\"uuid\\":\\"5c5ae87c-3b8b-2786-45b8-b16b969f8e7a\\",\\"hour\\":\\"23\\",\\"dt\\":\\"20190424\\",\\"minute\\":\\"17\\",\\"user_agent\\":\\"Safari Mozilla\\\\/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit\\\\/605.1.15 (KHTML, like Gecko) Mobile\\\\/15E148 mfwappcode\\\\/cn.mafengwo.www mfwappver\\\\/9.3.8 mfwappdevver\\\\/D1908.0 mfwjssdk\\\\/1.1 mfwappjsapi\\\\/1.7\\",\\"mfw_env\\":\\"product\\",\\"attr\\":{\\"_tpt\\":\\"\\\\u4fe1\\\\u606f\\\\u6d41\\",\\"prm_id\\":\\"\\",\\"_tpa\\":\\"\\\\u542f\\\\u52a8_\\\\u9996\\\\u9875\\",\\"item_name\\":\\"\\\\u673a\\\\u7968\\\\u706b\\\\u8f66\\\\u7968\\",\\"item_source\\":\\"\\",\\"_turi\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/index?banner=1&channel_id=55&theme=1\\",\\"_tp\\":\\"\\\\u9996\\\\u9875\\",\\"item_id\\":\\"\\",\\"item_uri\\":\\"http:\\\\/\\\\/m.mafengwo.cn\\\\/nb\\\\/public\\\\/sharejump.php?type=1500&source=app_home\\",\\"_tid\\":\\"B66086D2EA134BB3B7C957636E5F011C\\",\\"item_type\\":\\"\\",\\"_tpos\\":\\"index.index.index_icon.2\\",\\"show_cycle_id\\":\\"30261740-A01E-46D5-BBF7-24713BF03F69\\",\\"_tl\\":\\"1\\",\\"_tpre\\":\\"{\\\\\\"_turi\\\\\\":\\\\\\"http:\\\\\\\\\\\\/\\\\\\\\\\\\/app.mafengwo.cn\\\\\\\\\\\\/launch\\\\\\",\\\\\\"_tl\\\\\\":\\\\\\"0\\\\\\",\\\\\\"_tp\\\\\\":\\\\\\"\\\\u542f\\\\u52a8\\\\\\",\\\\\\"_tid\\\\\\":\\\\\\"C71450F83A6340A3BD9093E41D71FA42\\\\\\",\\\\\\"_tpi\\\\\\":\\\\\\"\\\\u542f\\\\u52a8\\\\\\",\\\\\\"_tpt\\\\\\":\\\\\\"\\\\u6b63\\\\u5e38\\\\\\"}\\",\\"pos_id\\":\\"index.index.index_icon.2\\",\\"_tpi\\":\\"40A0CE0E1CB243A399F70BEFDA8A5789\\",\\"module_name\\":\\"\\\\u997c\\\\u56fe\\",\\"sign_valid\\":1,\\"_client_upload_time\\":1556119062,\\"_dc_collect_audit_sdk_in\\":1556119062335,\\"_dc_collect_audit_sdk_out\\":1556119062347}}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我可以在这个站点上取消转义它,它会生成有效的 json:

\n\n
{\n  "uid": "93",\n  "channel_origin": "",\n  "sys_ver": "12.2",\n  "carrier_network_code": "02",\n  "rpn": "\xe9\xa6\x96\xe9\xa1\xb5",\n  "app_ver": "9.3.8",\n  "event_time": 1556119062,\n  "carrier": "\xe4\xb8\xad\xe5\x9b\xbd\xe7\xa7\xbb\xe5\x8a\xa8",\n  "event_guid": "E36FE60BFBFF405680906E3F95EC3953",\n  "ppn": "\xe5\x90\xaf\xe5\x8a\xa8",\n  "device_token": "d323828f196a3ca665fa296a2a5f23a57cce83963328b73bd2cf78a7d55575ef",\n  "day_first": "442",\n  "abtest": "{\\"app_home_change\\":\\"b\\",\\"app_icon_change\\":\\"b\\"}",\n  "pn": "\xe9\xa6\x96\xe9\xa1\xb5",\n  "puri": "http://app.mafengwo.cn/launch",\n  "day_before": "1",\n  "hardware_model": "iPhone10,3",\n  "lng": "114.427362",\n  "launch_guid": "69904D0912C943AC9D924AC8469CEA4E",\n  "app_code": "cn.mafengwo.www",\n  "_launch_source_history": "[]",\n  "idfv": "20587F0C-0F6E-4CD2-94C3-CE3A10CF0EFD",\n  "open_udid": "72F69B19-E612-4A4B-9D2F-8BB6C890421B",\n  "carrier_country_code": "cn",\n  "dev_ver": "D1908.0",\n  "device_type": "ios",\n  "ref": "http://app.mafengwo.cn/index?banner=1&channel_id=55&theme=1",\n  "lat": "27.818787",\n  "event_code": "show_index",\n  "idfa": "DC7D35C1-C81B-4884-96C5-C6BAE83FA7DA",\n  "uri": "http://app.mafengwo.cn/index?banner=1&channel_id=55&theme=1",\n  "channel_code": "",\n  "net": "1",\n  "_is_foreground": "1",\n  "sdk_ver": "20161011",\n  "_event_hash": "8ae3b940026375b8252e689113fc8c43f5bfddcf",\n  "ip": "39.176.24.64",\n  "umddid": 14696,\n  "topic": "mobile_event",\n  "ctime": 1556119062,\n  "ip_s": "node001026",\n  "datetime": "2019-04-24T23:17:42+08:00",\n  "uuid": "5c5ae87c-3b8b-2786-45b8-b16b969f8e7a",\n  "hour": "23",\n  "dt": "20190424",\n  "minute": "17",\n  "user_agent": "Safari Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 mfwappcode/cn.mafengwo.www mfwappver/9.3.8 mfwappdevver/D1908.0 mfwjssdk/1.1 mfwappjsapi/1.7",\n  "mfw_env": "product",\n  "attr": {\n    "_tpt": "\xe4\xbf\xa1\xe6\x81\xaf\xe6\xb5\x81",\n    "prm_id": "",\n    "_tpa": "\xe5\x90\xaf\xe5\x8a\xa8_\xe9\xa6\x96\xe9\xa1\xb5",\n    "item_name": "\xe6\x9c\xba\xe7\xa5\xa8\xe7\x81\xab\xe8\xbd\xa6\xe7\xa5\xa8",\n    "item_source": "",\n    "_turi": "http://app.mafengwo.cn/index?banner=1&channel_id=55&theme=1",\n    "_tp": "\xe9\xa6\x96\xe9\xa1\xb5",\n    "item_id": "",\n    "item_uri": "http://m.mafengwo.cn/nb/public/sharejump.php?type=1500&source=app_home",\n    "_tid": "B66086D2EA134BB3B7C957636E5F011C",\n    "item_type": "",\n    "_tpos": "index.index.index_icon.2",\n    "show_cycle_id": "30261740-A01E-46D5-BBF7-24713BF03F69",\n    "_tl": "1",\n    "_tpre": "{\\"_turi\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/launch\\",\\"_tl\\":\\"0\\",\\"_tp\\":\\"\xe5\x90\xaf\xe5\x8a\xa8\\",\\"_tid\\":\\"C71450F83A6340A3BD9093E41D71FA42\\",\\"_tpi\\":\\"\xe5\x90\xaf\xe5\x8a\xa8\\",\\"_tpt\\":\\"\xe6\xad\xa3\xe5\xb8\xb8\\"}",\n    "pos_id": "index.index.index_icon.2",\n    "_tpi": "40A0CE0E1CB243A399F70BEFDA8A5789",\n    "module_name": "\xe9\xa5\xbc\xe5\x9b\xbe",\n    "sign_valid": 1,\n    "_client_upload_time": 1556119062,\n    "_dc_collect_audit_sdk_in": 1556119062335,\n    "_dc_collect_audit_sdk_out": 1556119062347\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我怎样才能用Java做到这一点呢?

\n\n

我尝试StringEscapeUtils.unescapeJava过 apache commons,但它会产生类似以下内容:

\n\n
{"channel_origin":"","sys_ver":"12.1.4","carrier_network_code":"00","rpn":"?????","app_ver":"9.3.8","event_time":1556118681,"carrier":"????","event_guid":"EE9ED76F27FC4CCB855ADC80B03E4757","ppn":"??","device_token":"9244d63150045f19f53980eb617b25755f444cb94a5a24b761d6e0b711ecb5be","day_first":"4","abtest":"{"app_home_change":"b","app_icon_change":"a"}","pn":"??","puri":"http://app.mafengwo.cn/launch","day_before":"1","hardware_model":"iPhone11,8","lng":"121.512072","launch_guid":"17787EB05F5C4B12A5BE9C6939725A07","app_code":"cn.mafengwo.www","_launch_source_history":"[]","idfv":"26E08BD5-6F71-4ABB-8F76-4DFC181F930A","open_udid":"FC07FB12-C1BD-4307-8B51-00E3E76D35E1","carrier_country_code":"cn","dev_ver":"D1908.0","device_type":"ios","ref":"http://app.mafengwo.cn/launch/ads","lat":"31.071058","event_code":"show_index","idfa":"FC07FB12-C1BD-4307-8B51-00E3E76D35E1","uri":"http://app.mafengwo.cn/index?banner=0&channel_id=55&theme=0","channel_code":"","net":"4","_is_foreground":"1","sdk_ver":"20161011","_event_hash":"5b788e9db5623e5bd1f90d7b52991bba889bf425","ip":"117.136.8.71","umddid":10099,"topic":"mobile_event","ctime":1556118694,"ip_s":"node011107","datetime":"2019-04-24T23:11:34+08:00","uuid":"5cbb0cbe-7620-dd88-9cbb-f4942994e47c","hour":"23","dt":"20190424","minute":"11","user_agent":"Safari Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 mfwappcode/cn.mafengwo.www mfwappver/9.3.8 mfwappdevver/D1908.0 mfwjssdk/1.1 mfwappjsapi/1.7","mfw_env":"product","attr":{"_tpt":"???","prm_id":"","_tpa":"??_??","item_name":"?????","item_source":"","_turi":"http://app.mafengwo.cn/index?banner=0&channel_id=55&theme=0","_tp":"??","item_id":"","item_uri":"http://m.mafengwo.cn/nb/public/sharejump.php?type=1500&source=app_home","_tid":"378862F0BE7E437D874782155EAA5980","item_type":"","_tpos":"index.index.index_icon.2","show_cycle_id":"5C3BE7DF-B62F-4F78-A878-D178D3710649","_tl":"1","_tpre":"{"_turi":"http:\\/\\/app.mafengwo.cn\\/launch","_tl":"0","_tp":"??","_tid":"39E918A8F82D44E7B7A8BF122CFA235E","_tpi":"??","_tpt":"??"}","pos_id":"index.index.index_icon.2","_tpi":"2D898392135245F49055797FEC4FED7C","module_name":"??","sign_valid":1,"_client_upload_time":1556118693,"_dc_collect_audit_sdk_in":1556118694452,"_dc_collect_audit_sdk_out":1556118694488}}\n
Run Code Online (Sandbox Code Playgroud)\n\n

其中更改\\\\\\"",而不是正确的一个\\"

\n

T.J*_*der 6

假设您确实拥有该字符串,看起来有人对其进行了双重编码(即,将结构序列化为 JSON,然后再次将该字符串序列化为 JSON)。如果是这样,您应该能够将其解析为 JSON 并返回一个字符串 - 然后您可以将其解析为 JSON 以获得结构。

我没用过杰克逊,但类似

ObjectMapper objectMapper = new ObjectMapper();
String unwrappedJSON = objectMapper.readValue(json, String.class);
YourClass result = objectMapper.readValue(unwrappedJSON, YourClass.class);
Run Code Online (Sandbox Code Playgroud)