dongling5411 2017-03-05 12:52
浏览 103
已采纳

utf-8中的错误解码字符串中的JSON解码器

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条回答 默认 最新

  • doudi2833 2017-03-06 04:05
    关注
    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
    ", x,x,x)
            tmp = append(tmp, rune(x))
        }
        fmt.Printf("Result output: %v : `%s`
    ", 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?