Go中的JSON解码器在解码UTF-8字符串时出现错误。

huangapple go评论88阅读模式
英文:

JSON decoder in Go wrong decoding string in UTF-8

问题

我在Go语言中遇到了JSON解码器的问题。我有一个客户端(dotnet core)和一个服务器(Go),它们通过套接字进行通信。编码设置为utf-8。在服务器端解码后,字符串中的一个部分不是正确的格式。

Go解码代码:

buf := make([]byte, bufferSize)
_, err := conn.Read(buf)
if err != nil {
    fmt.Println("Error reading:", err.Error())
}

s := string(buf[:])
r := strings.NewReader(s)
d := json.NewDecoder(r)

request := Request{}
d.Decode(&request)

在解码之前,变量s包含正确的字符串:https://i.stack.imgur.com/EUYF3.png,args包含正确的单词"zárít"。

解码后,字符串被破坏,包含了这个:https://i.stack.imgur.com/Zqan8.png

我不理解第二张图片中的"...+2 more"的表示方式,也不知道如何正确解码这个字符串。

编辑:

这个问题的核心可以通过以下代码重现:

package main

import (
    "fmt"
    "encoding/json"
    "strings"
)

type Request struct {
    Arg     string
}

func main() {
    s := "{\"Arg\": \"zárít\"}"
    r := strings.NewReader(s)
    d := json.NewDecoder(r)

    request := Request{}
    d.Decode(&request)

    for i := 0; i < len(request.Arg); i++ {
        char := request.Arg[i]
        fmt.Print(string(char))
    }
    fmt.Println()
    fmt.Println(request.Arg)
}

为什么输出结果不一样?我应该如何得到相同的结果?

英文:

I have a problem with json decoder in Go. I have client (dotnet core) and server (go) which are communicate via sockets. Encoding is setted to utf-8. After decoding on server side is not one of string in correct format.

Go decoding code:

buf := make([]byte, bufferSize)
_, err := conn.Read(buf)
if err != nil {
	fmt.Println(&quot;Error reading:&quot;, err.Error())
}

s := string(buf[:])
r := strings.NewReader(s)
d := json.NewDecoder(r)

request := Request{}
d.Decode(&amp;request)

Variable s contains correct string before decoding: https://i.stack.imgur.com/EUYF3.png and args contains correct word "zářit".

After decoding is string broken and contains this: https://i.stack.imgur.com/Zqan8.png

I don't understand representation ...+2 more from second image and I don't know how to decode this string correct way.

EDIT:

The core of the problem can be reproduced by this code:

package main

import (
    &quot;fmt&quot;
    &quot;encoding/json&quot;
    &quot;strings&quot;
)

type Request struct {
    Arg     string
}

func main() {
    s := &quot;{\&quot;Arg\&quot;: \&quot;z&#225;řit\&quot;}&quot;
    r := strings.NewReader(s)
    d := json.NewDecoder(r)

    request := Request{}
    d.Decode(&amp;request)

    for i := 0; i &lt; len(request.Arg); i++ {
	    char := request.Arg[i]
	    fmt.Print(string(char))
    }
    fmt.Println()
    fmt.Println(request.Arg)
}

Why output is not the same? How I should get the same result?

答案1

得分: 0

main

导入 (
"fmt"
"encoding/json"
"strings"
)

类型请求结构 {
Arg string
}

功能主要() {
s := " {"Arg": "zářit"}"
r := strings.NewReader(s)
d := json.NewDecoder(r)

请求 := 请求结构{}
d.Decode(&请求)

对于 i := 0; i < len(请求.Arg); i++ {
	char := 请求.Arg[i]
	fmt.Print(string(char))
}

//正确显示示例
var tmp []rune
对于 _, x := 范围 请求.Arg {
	fmt.Printf("%x : %v : %c\n", x, x, x)
	tmp = append(tmp, rune(x))
}
fmt.Printf("结果输出:%v : `%s`\n", tmp, string(tmp))


fmt.Println()
fmt.Println(请求.Arg)

}

输出

/usr/local/go/bin/go run /home/spouk/go/src/simple/translator

/error_json_code.go
zářit7a : 122 : z
e1 : 225 : á
159 : 345 : ř
69 : 105 : i
74 : 116 : t
结果输出:[122 225 345 105 116] : z&#225;řit

zářit

进程以退出码0结束

英文:
main

import (
	&quot;fmt&quot;
	&quot;encoding/json&quot;
	&quot;strings&quot;
)

type Request struct {
	Arg     string
}

func main() {
	s := &quot;{\&quot;Arg\&quot;: \&quot;z&#225;řit\&quot;}&quot;
	r := strings.NewReader(s)
	d := json.NewDecoder(r)


	request := Request{}
	d.Decode(&amp;request)

	for i := 0; i &lt; len(request.Arg); i++ {
		char := request.Arg[i]
		fmt.Print(string(char))
	}

	//example correct show
	var tmp []rune
	for _, x := range request.Arg {
		fmt.Printf(&quot;%x : %v : %c\n&quot;, x,x,x)
		tmp = append(tmp, rune(x))
	}
	fmt.Printf(&quot;Result output: %v : `%s`\n&quot;, tmp, string(tmp))
	

	fmt.Println()
	fmt.Println(request.Arg)

}

output

/usr/local/go/bin/go run /home/spouk/go/src/simple/translator

/error_json_code.go
z&#195;&#161;&#197;™it7a : 122 : z
e1 : 225 : &#225;
159 : 345 : ř
69 : 105 : i
74 : 116 : t
Result output: [122 225 345 105 116] : `z&#225;řit`

z&#225;řit

Process finished with exit code 0

huangapple
  • 本文由 发表于 2017年3月5日 20:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/42608567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定