亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 從 JSON 字符串數據中讀取 html 標簽 (<>) 作為 < 和 >

Golang 從 JSON 字符串數據中讀取 html 標簽 (<>) 作為 < 和 >

Go
千巷貓影 2022-11-23 16:04:53
我有一個基本的 Web 服務器,它從 JSON 帖子數據庫中呈現博客帖子,其中主要段落是從 JSON 字符串數組構建的。我試圖找到一種方法來輕松地對新行或換行符進行編碼,但發現這些值的編碼如何從 JSON 更改為 GoLang,最后更改為我的 HTML 網頁時遇到了很多困難。當我嘗試用換行符對我的 JSON 進行編碼時,我發現我必須對它們進行編碼\\n而不是僅僅\n為了讓它們實際出現在我的頁面上。然而,一個問題是它們只是作為文本出現,而不是換行符。然后我嘗試研究\n將連接的字符串數組的部分替換為<br>標簽的方法,但是我找不到任何方法來使用 go 來執行此操作,并轉向嘗試在 javascript 中這樣做。盡管我在我的 HTML 鏈接中推遲了對我的 javascript 的調用,但這也不起作用。這是那個javascript:var title = window.document.getElementById("title");var timestamp = window.document.getElementById("timestamp");var sitemap = window.document.getElementById("sitemap");var main = window.document.getElementById("main");var contact_form = window.document.getElementById("contact-form");var content_info = window.document.getElementById("content-info");var str = main.innerHTML;function replaceNewlines() {    // Replace the \n with <br>    str = str.replace(/(?:\r\n|\r|\n)/g, "<br>");    // Update the value of paragraph    main.innerHTML = str;}這是我的 HTML:<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Dynamic JSON Events</title>    <link rel="stylesheet" href="/blogtemplate.css"></style></head><body>    <section id="title">        <h1 id="text-title">{{.Title}}</h1>        <time id="timestamp">            {{.Timestamp}}        </time>    </section>    <nav role="navigation" id="site-nav">        <ul id="sitemap">        </ul>    </nav>    <main role="main" id="main">        {{.ParsedMain}}    </main>    <footer role="contentinfo" id="footer">        <form id="contact-form" role="form">        <address>            Contact me by <a id="my-email" href="mailto:[email protected]" class="my-email">e-mail</a>        </address>        </form>    </footer><script defer src="/blogtemplate.js"></script></body></html>然后,我最終轉而嘗試將<br>標簽硬編碼到我的 json 數據中,發現當它最終到達瀏覽器時,它只是呈現為 < 和 >。我對這種編碼過程感到非常沮喪,不斷導致我在創建換行符和換行符時出現問題。如何在我的 JSON 字符串數據中輕松地包含我想要的換行符?
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

您可以嘗試在模板內遍歷數組并為數組的每個元素生成 ap 標記。這樣就不需要在 go 中編輯主數組。


模板:


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Dynamic JSON Events</title>

    <link rel="stylesheet" href="/blogtemplate.css"></style>

</head>

<body>

    <section id="title">

        <h1 id="text-title">{{.Title}}</h1>

        <time id="timestamp">

            {{.Timestamp}}

        </time>

    </section>

    <nav role="navigation" id="site-nav">

        <ul id="sitemap">

        </ul>

    </nav>

    <main role="main" id="main">

        {{range $element := .Main}} <p>{{$element}}</p> {{end}}

    </main>

    <footer role="contentinfo" id="footer">

        <form id="contact-form" role="form">

        <address>

            Contact me by <a id="my-email" href="mailto:[email protected]" class="my-email">e-mail</a>

        </address>

        </form>

    </footer>

<script defer src="/blogtemplate.js">

</script>

</body>

</html>


查看完整回答
反對 回復 2022-11-23
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

如果您希望 JSON 文檔包含 HTML,請執行以下操作:

  1. 將 ParsedMain更改為鍵入html.HTML

     type BlogPost struct {
         ...     ParsedMain html.HTML
     }
  2. 分配字段時轉換為該類型:

     post.ParsedMain = html.HTML(strings.Join(post.Main, "")).
  3. 替換文檔中的``\ with
    n`。

如果不受信任的用戶可以輸入 JSON 文檔數據,那么應用程序應該根據允許列表過濾 HTML 標記和屬性。這可以防止攻擊者通過腳本注入發起攻擊。


如果要將 JSON 文檔中的換行符轉換為 HTML 中的換行符,請執行以下操作:


更改文檔以包含換行符:\\n-> \n。


在服務器或客戶端上,用 HTML 換行符替換換行符。為防止腳本注入攻擊,請在插入<br>. 以下是在服務器上執行此操作的方法:


 type BlogPost struct {

     ...

     ParsedMain html.HTML

 }


 escapedAndJoined := html.Escaper(post.Main...)

 post.ParsedMain = html.HTML(strings.ReplaceAll(escapedAndJoined, "\n", "<br>"))).

您可能想使用<p>而不是<br>.


查看完整回答
反對 回復 2022-11-23
  • 2 回答
  • 0 關注
  • 247 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號