簡體   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