how do i encode http response bytes in utf8 correctly?

JSON decoder in Go can deal with encoded symbols, you need to unmarshal payload in struct or other value:

https://play.golang.org/p/Mt-phU4kxfy

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    payload := []byte(`{"error_code":201,"error_name":"\u041f\u043e\u043b ... "}`)
    var p interface{}

    _ = json.Unmarshal(payload, &p)
    fmt.Printf("%#v\n", p) // map[string]interface {}{"error_code":201, "error_name":"Пол ... "}

    var e struct {
        ErrorCode int    `json:"error_code"`
        ErrorName string `json:"error_name"`
    }

    _ = json.Unmarshal(payload, &e)
    fmt.Println(e.ErrorCode, e.ErrorName) // 201 Пол ...
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top