英文:
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("Error reading:", err.Error())
}
s := string(buf[:])
r := strings.NewReader(s)
d := json.NewDecoder(r)
request := Request{}
d.Decode(&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 (
"fmt"
"encoding/json"
"strings"
)
type Request struct {
Arg string
}
func main() {
s := "{\"Arg\": \"zářit\"}"
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)
}
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ářit
zářit
进程以退出码0结束
英文:
main
import (
"fmt"
"encoding/json"
"strings"
)
type Request struct {
Arg string
}
func main() {
s := "{\"Arg\": \"zářit\"}"
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))
}
//example correct show
var tmp []rune
for _, x := range request.Arg {
fmt.Printf("%x : %v : %c\n", x,x,x)
tmp = append(tmp, rune(x))
}
fmt.Printf("Result output: %v : `%s`\n", 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áÅit7a : 122 : z
e1 : 225 : á
159 : 345 : ř
69 : 105 : i
74 : 116 : t
Result output: [122 225 345 105 116] : `zářit`
zářit
Process finished with exit code 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论