2015-11-05 57 views
0

我从数据库中获取一些值并生成一些JSON。这个JSON然后被客户端使用。但只要从数据库中读出的字符串出现换行符,客户端就无法解释JSON,并且Web应用程序不显示任何数据。数据库中的字符串如下所示:由换行符破坏的JSON

Test Action Item ZusammenführenTest Action Item ZusammenführenTest Action Item ZusammenführenTest Action Item ZusammenführenTest Action Item ZusammenführenTest Action Item Zusammenführen 
fsd 
fsa 
df 
asdf 
as 
dfs 
fd 
sad 
f 
sadf 
sad 

所以有几个换行符。由后台java代码生成的JSON对象如下所示:

{ "id": 61, 
    "status": "erledigtEntw", 
    "text": "KORE_AI_1", 
    "beschreibung": "Test Action Item ZusammenführenTest Action Item 

ZusammenführenTest Action Item ZusammenführenTest Action Item ZusammenführenTest Action Item ZusammenführenTest Action Item Zusammenführen 
fsd 
fsa 
df 
asdf 
as 
dfs 
fd 
sad 
f 
sadf 
sad", 
     "tags": "KORE" 
     }, 

并且似乎存在问题。也许客户端不会将其识别为格式良好的JSON。我怎样才能改变这一点,以便输出再次形成?如果可能的话,我如何跟踪和编码换行符?

+0

的[在JSON多行字符串(http://stackoverflow.com/questions/2392766/multiline-strings-in-json) – Andreas

回答

0

用“\ n”或“\ r \ n”替换所有换行符。

yourVar.replaceAll("\n", "\\n"); 
+0

我如何检测这些换行符在Java中与\替换它们可能的复制N + –