Golang 从 JSON 字符串数据中读取 html 标签 (<>) 作为 < 和 >

我有一个基本的 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:antonhibl11@gmail.com" class="my-email">e-mail</a>

        </address>

        </form>

    </footer>

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

</script>

</body>

</html>

然后,我最终转而尝试将<br>标签硬编码到我的 json 数据中,发现当它最终到达浏览器时,它只是呈现为 < 和 >。我对这种编码过程感到非常沮丧,不断导致我在创建换行符和换行符时出现问题。如何在我的 JSON 字符串数据中轻松地包含我想要的换行符?


千巷猫影
浏览 192回答 2
2回答

肥皂起泡泡

您可以尝试在模板内遍历数组并为数组的每个元素生成 ap 标记。这样就不需要在 go 中编辑主数组。模板:<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="IE=edge">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Dynamic JSON Events</title>&nbsp; &nbsp; <link rel="stylesheet" href="/blogtemplate.css"></style></head><body>&nbsp; &nbsp; <section id="title">&nbsp; &nbsp; &nbsp; &nbsp; <h1 id="text-title">{{.Title}}</h1>&nbsp; &nbsp; &nbsp; &nbsp; <time id="timestamp">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{.Timestamp}}&nbsp; &nbsp; &nbsp; &nbsp; </time>&nbsp; &nbsp; </section>&nbsp; &nbsp; <nav role="navigation" id="site-nav">&nbsp; &nbsp; &nbsp; &nbsp; <ul id="sitemap">&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </nav>&nbsp; &nbsp; <main role="main" id="main">&nbsp; &nbsp; &nbsp; &nbsp; {{range $element := .Main}} <p>{{$element}}</p> {{end}}&nbsp; &nbsp; </main>&nbsp; &nbsp; <footer role="contentinfo" id="footer">&nbsp; &nbsp; &nbsp; &nbsp; <form id="contact-form" role="form">&nbsp; &nbsp; &nbsp; &nbsp; <address>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Contact me by <a id="my-email" href="mailto:antonhibl11@gmail.com" class="my-email">e-mail</a>&nbsp; &nbsp; &nbsp; &nbsp; </address>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </footer><script defer src="/blogtemplate.js"></script></body></html>

jeck猫

如果您希望 JSON 文档包含 HTML,请执行以下操作:将 ParsedMain更改为键入html.HTML:&nbsp;type&nbsp;BlogPost&nbsp;struct&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ParsedMain&nbsp;html.HTML &nbsp;}分配字段时转换为该类型:&nbsp;post.ParsedMain&nbsp;=&nbsp;html.HTML(strings.Join(post.Main,&nbsp;"")).替换文档中的``\&nbsp;withn`。如果不受信任的用户可以输入 JSON 文档数据,那么应用程序应该根据允许列表过滤 HTML 标记和属性。这可以防止攻击者通过脚本注入发起攻击。如果要将 JSON 文档中的换行符转换为 HTML 中的换行符,请执行以下操作:更改文档以包含换行符:\\n-> \n。在服务器或客户端上,用 HTML 换行符替换换行符。为防止脚本注入攻击,请在插入<br>. 以下是在服务器上执行此操作的方法:&nbsp;type BlogPost struct {&nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp;ParsedMain html.HTML&nbsp;}&nbsp;escapedAndJoined := html.Escaper(post.Main...)&nbsp;post.ParsedMain = html.HTML(strings.ReplaceAll(escapedAndJoined, "\n", "<br>"))).您可能想使用<p>而不是<br>.
打开App,查看更多内容
随时随地看视频慕课网APP