簡體   English   中英

如何在 JSON 中添加換行符?

[英]how can i add Line break in JSON?

我想做的是我想在兩者之間添加一個換行符

remoteMessage.notification.title + remoteMessage.notification.body

標題和正文

如果我使用我的代碼屏幕視圖顯示這樣

在此處輸入圖像描述

這是我的代碼

useEffect(() => {
  const unsubscribe = messaging().onMessage(async (remoteMessage) => {
    console.log(JSON.stringify(remoteMessage));
    Alert.alert(
      "A new FCM message arrived!",
      JSON.stringify(
        remoteMessage.notification.title + remoteMessage.notification.body
      )
    );
  });

  return unsubscribe;
}, []);

我該如何修復我的代碼? 如果我想這樣顯示?

title 
body

您可以使用\n ,否則,您可以嘗試在兩個文本之間使用<br/>

useEffect(() => {
  const unsubscribe = messaging().onMessage(async (remoteMessage) => {
    console.log(JSON.stringify(remoteMessage));
    Alert.alert(
      "A new FCM message arrived!",
      JSON.stringify(
        remoteMessage.notification.title +"<br/>"+ remoteMessage.notification.body
      )
    );
  });

  return unsubscribe;
}, []);

如果你知道如何處理 CSS(我希望你是前端開發人員),你可以使用 css 的 white-space: break-spaces 和 '\n' 的組合來換行。

你的.json 文件

{
   text: "I want to show you \nthe text that is line break."
}

你的.css 文件

p {
   white-space: break-spaces;
}

也許它看起來像這樣。

I want to show you
the text that is line break.

您可以使用\nwhite-space: pre-line;CSS

 const text = "Title\nBody" function App() { return <div className="pre-line">{text}</div> } ReactDOM.render(<App />, document.getElementById('root'))
 .pre-line { white-space: pre-line; }
 <script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script> <body> <div id="root"></div> </body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM