首页后端开发其他后端知识Go语言的json如何应用,有哪些使用问题要注意

Go语言的json如何应用,有哪些使用问题要注意

时间2024-03-26 03:40:03发布访客分类其他后端知识浏览1089
导读:这篇文章主要给大家介绍“Go语言的json如何应用,有哪些使用问题要注意”的相关知识,下文通过实际案例向大家展示操作过程,内容简单清晰,易于学习,有这方面学习需要的朋友可以参考,希望这篇“Go语言的json如何应用,有哪些使用问题要注意”文...
这篇文章主要给大家介绍“Go语言的json如何应用,有哪些使用问题要注意”的相关知识,下文通过实际案例向大家展示操作过程,内容简单清晰,易于学习,有这方面学习需要的朋友可以参考,希望这篇“Go语言的json如何应用,有哪些使用问题要注意”文章能对大家有所帮助。
     




下面由golang教程栏目给大家分享一个Go json 踩坑记录,希望对需要的朋友有所帮助!

JSON,一种轻量级的资料交换语言,该语言以易于让人阅读的文字为基础,用来传输由属性值或者序列性的值组成的数据对象。现已经被广泛应用,Go 当然也提供了完备的支持,通过 encoding/json 便可方便的序列化及反序列化 JSON 数据。但是也有些关键点需要额外注意下。

Go 可使用 json.Marshal() 便捷的获取 JSON 数据,查看该函数对应的 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 转码成 “\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 字符串后接一个 换行符,该函数源码如下:

// 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
}
    

可以看出,该函数在进行序列化后又写入了一个 \n 字符。如果不需要该字符,则需要额外的将其剔除:

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

以上就是关于“Go语言的json如何应用,有哪些使用问题要注意”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注网络,小编每天都会为大家更新不同的知识。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Go语言的json如何应用,有哪些使用问题要注意
本文地址: https://pptw.com/jishu/653235.html
eclipse中使用Bootstrap的方法及操作是什么 PHP中代码加密怎样做,有多少种方法?

游客 回复需填写必要信息