繁体   English   中英

换行符破坏了JSON

[英]JSON destroyed by line breaks

我从数据库中获取了一些值并生成了JSON。 然后,客户端将使用此JSON。 但是,一旦从数据库中读取的字符串中出现换行符,客户端就无法解释JSON,并且Web应用程序不会显示任何数据。 数据库中的字符串如下所示:

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。 如何更改此设置,以便再次形成正确的输出? 如果可能的话,如何跟踪和编码换行符?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM