Go中json踩坑記錄的示例分析

這篇文章主要介紹Go中json踩坑記錄的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站是一家專業(yè)提供樂陵企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站制作、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為樂陵眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。

                       

JSON,一種輕量級的資料交換語言,該語言以易于讓人閱讀的文字為基礎(chǔ),用來傳輸由屬性值或者序列性的值組成的數(shù)據(jù)對象?,F(xiàn)已經(jīng)被廣泛應(yīng)用,Go 當(dāng)然也提供了完備的支持,通過 encoding/json 便可方便的序列化及反序列化 JSON 數(shù)據(jù)。但是也有些關(guān)鍵點需要額外注意下。

Go 可使用 json.Marshal() 便捷的獲取 JSON 數(shù)據(jù),查看該函數(shù)對應(yīng)的 godoc 文檔,里面有這么一段話:

String values encode as JSON strings coerced to valid UTF-8, replacing invalid bytes with the Unicode replacement rune. So that the JSON will be safe to embed inside HTML <script> tags, the string is encoded using HTMLEscape, which replaces "<", ">", "&", U+2028, and U+2029 are escaped to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029". This replacement can be disabled when using an Encoder, by calling SetEscapeHTML(false).

json.Marshal() 在進行序列化時,會進行 HTMLEscape 編碼,會將 “<”, “>”, “&”, U+2028, 及 U+2029 轉(zhuǎn)碼成 “\u003c”,”\u003e”, “\u0026”, “\u2028”, 和 “\u2029”。這在正常使用時是沒有問題的,但如果在對接第三方需要對 JSON 字符串進行取摘要比對時,如果一方未進行 HTMLEscape 編碼,取出的摘要便會天差地別。上述文檔中也給出了解決方法,通過 SetEscapeHTML(false) 來禁用便可。方法如下:

bf := bytes.NewBuffer([]byte{})jsonEncoder := json.NewEncoder(bf)jsonEncoder.SetEscapeHTML(false)_ = jsonEncoder.Encode(body)jsonStr := bf.String()

但是這樣使用仍然會有些問題,json.Encoder.Encode() 會在 JSON 字符串后接一個 換行符,該函數(shù)源碼如下:

// Encode writes the JSON encoding of v to the stream,
// followed by a newline character.
//
// See the documentation for Marshal for details about the
// conversion of Go values to JSON.
func (enc *Encoder) Encode(v interface{}) error {
    if enc.err != nil {
        return enc.err
    }
    e := newEncodeState()
    err := e.marshal(v, encOpts{escapeHTML: enc.escapeHTML})
    if err != nil {
        return err
    }

    // Terminate each value with a newline.
    // This makes the output look a little nicer
    // when debugging, and some kind of space
    // is required if the encoded value was a number,
    // so that the reader knows there aren't more
    // digits coming.
    e.WriteByte('\n')

    b := e.Bytes()
    if enc.indentPrefix != "" || enc.indentValue != "" {
        if enc.indentBuf == nil {
            enc.indentBuf = new(bytes.Buffer)
        }
        enc.indentBuf.Reset()
        err = Indent(enc.indentBuf, b, enc.indentPrefix, enc.indentValue)
        if err != nil {
            return err
        }
        b = enc.indentBuf.Bytes()
    }
    if _, err = enc.w.Write(b); err != nil {
        enc.err = err
    }
    encodeStatePool.Put(e)
    return err
}

可以看出,該函數(shù)在進行序列化后又寫入了一個 \n 字符。如果不需要該字符,則需要額外的將其剔除:

jsonStr := string(bf.Bytes()[:bf.bf.Len()])

以上是“Go中json踩坑記錄的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:Go中json踩坑記錄的示例分析
地址分享:http://www.muchs.cn/article12/jpjjgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計App設(shè)計、全網(wǎng)營銷推廣虛擬主機、動態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)