【问题标题】:How can I add a line return into json?如何在 json 中添加行返回?
【发布时间】:2021-07-29 08:28:09
【问题描述】:

我有一个看似简单的问题。如何在 json 获取的数据中插入换行符。此 json 是从远程 firebase 服务器传递的。

这是我的json

    [ {
  "body" : "Hello, my name is __________________ (state your name). \nI wish this text to be on a new line",
  "hideOrShow" : true,
  "icon" : "./assets/imgs/test.svg",
  "title" : "Test"
} ]

但我的代码一直将正文打印为一行文本。不添加新行。

我做错了什么?

更新:我正在使用 ionic 5 并像这样打印 json:

        <ion-item class="ion-text-wrap text-import">
      {{sanitizeAndUnescapeHtml(s.body)}}
    </ion-item>

(来自我的 .ts 文件:

    sanitizeAndUnescapeHtml(body: string): string {
  const sanitized = this.domSanitzer.sanitize(SecurityContext.HTML, body);
  // console.log('s', sanitized);
  const parsed = new DOMParser().parseFromString(sanitized, 'text/html');
  // console.log('p', parsed.textContent);
  return parsed.documentElement.textContent;
}

【问题讨论】:

标签: json


【解决方案1】:

您的问题很特殊,因为有两种数据类型。
所以按程序在类型上添加 \n 有点困难。
使用这个 json 格式化程序。

https://jsonformatter.curiousconcept.com/                                                

【讨论】:

    【解决方案2】:

    谢谢大家。解决方案是转义 HTML,然后更改此代码行:

      <div style="overflow-y: auto;" [innerHTML]="sanitizeAndUnescapeHtml(e.body)"></div>
    

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多