婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

熱門標(biāo)簽:江西轉(zhuǎn)化率高的羿智云外呼系統(tǒng) 學(xué)海導(dǎo)航地圖標(biāo)注 中國(guó)地圖標(biāo)注省會(huì)高清 南通如皋申請(qǐng)開(kāi)通400電話 廣州呼叫中心外呼系統(tǒng) 西部云谷一期地圖標(biāo)注 地圖標(biāo)注的汽車標(biāo) 浙江高速公路地圖標(biāo)注 高德地圖標(biāo)注口訣

go語(yǔ)言提供了json的編解碼包,json字符串作為參數(shù)值傳輸時(shí)發(fā)現(xiàn),json.Marshal生成json特殊字符、>、會(huì)被轉(zhuǎn)義。

type Test struct {
  Content   string
}
func main() {
  t := new(Test)
  t.Content = "http://www.baidu.com?id=123test=1"
  jsonByte, _ := json.Marshal(t)
  fmt.Println(string(jsonByte))
}
{"Content":"http://www.baidu.com?id=123\u0026test=1"}
Process finished with exit code 0

GoDoc描述

String values encode as JSON strings coerced to valid UTF-8,

replacing invalid bytes with the Unicode replacement rune.

The angle brackets “” and “>” are escaped to “\u003c” and “\u003e”

to keep some browsers from misinterpreting JSON output as HTML.

Ampersand “” is also escaped to “\u0026” for the same reason.

This escaping can be disabled using an Encoder that had SetEscapeHTML(false) alled on it.

json.Marshal 默認(rèn) escapeHtml 為true,會(huì)轉(zhuǎn)義 、>、

func Marshal(v interface{}) ([]byte, error) {
  e := encodeState{}
  err := e.marshal(v, encOpts{escapeHTML: true})
  if err != nil {
    return nil, err
  }
  return e.Bytes(), nil
}

解決方案

方法一:

content = strings.Replace(content, "\\u003c", "", -1)
content = strings.Replace(content, "\\u003e", ">", -1)
content = strings.Replace(content, "\\u0026", "", -1)

這種方式比較直接,硬性字符串替換。比較憨厚

方法二:

文檔中寫(xiě)到This escaping can be disabled using an Encoder that had SetEscapeHTML(false) alled on it.

我們先創(chuàng)建一個(gè)buffer用于存儲(chǔ)json

創(chuàng)建一個(gè)jsonencoder

設(shè)置html編碼為false

type Test struct {
  Content   string
}
func main() {
  t := new(Test)
  t.Content = "http://www.baidu.com?id=123test=1"
  bf := bytes.NewBuffer([]byte{})
  jsonEncoder := json.NewEncoder(bf)
  jsonEncoder.SetEscapeHTML(false)
  jsonEncoder.Encode(t)
  fmt.Println(bf.String())
}
{"Content":"http://www.baidu.com?id=123test=1"}
Process finished with exit code 0

查看文檔和源碼還是解決問(wèn)題的好方法。

以上這篇golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • golang語(yǔ)言如何將interface轉(zhuǎn)為int, string,slice,struct等類型
  • golang如何使用struct的tag屬性的詳細(xì)介紹
  • Golang 如何解析和生成json
  • golang使用json格式實(shí)現(xiàn)增刪查改的實(shí)現(xiàn)示例
  • golang 實(shí)現(xiàn)struct、json、map互相轉(zhuǎn)化

標(biāo)簽:德宏 貴州 許昌 吐魯番 曲靖 東營(yíng) 常州 保定

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法》,本文關(guān)鍵詞  golang,json.Marshal,特殊,html,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 和田市| 肥东县| 聂荣县| 大田县| 乐昌市| 淮阳县| 石棉县| 沂源县| 广宁县| 辽阳县| 桓台县| 内乡县| 大庆市| 桑日县| 佛山市| 湘潭县| 滦平县| 双流县| 潞城市| 巴彦县| 怀集县| 湛江市| 大名县| 岚皋县| 南昌市| 凤阳县| 孝感市| 广南县| 铅山县| 泸水县| 栖霞市| 庄河市| 吴桥县| 申扎县| 辽源市| 涟源市| 深圳市| 遂平县| 祁东县| 德化县| 玉龙|