问题记录1 json解析问题

问题:
json解析int类型不符合预期,使用json.NewDecoder解决。
示例如下:

package main

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

func main() {
	data1 := map[string]interface{}{}
	data1["id"] = int64(444109192883284478)
	data1["name"] = "xiaozhu"
	data2 := map[string]interface{}{}
	data2["id"] = int64(444109192883284477)
	data2["name"] = "xiaozhu2"

	data := make([]map[string]interface{}, 2)
	data[0] = data1
	data[1] = data2

	dataBytes, _ := json.Marshal(data)
	fmt.Printf("dataBytes: %v \n", string(dataBytes))

	var payload1 []map[string]interface{}
	var payload2 []map[string]interface{}
	json.Unmarshal(dataBytes, &payload1)
	fmt.Printf("payload1: %v \n", payload1)

	decoder := json.NewDecoder(bytes.NewReader(dataBytes))
	decoder.UseNumber()
	if err := decoder.Decode(&payload2); err == nil {
		fmt.Printf("payload2: %v \n", payload2)
	}
}

问题记录1 json解析问题_第1张图片

你可能感兴趣的:(问题记录,golang)