golang-json中的数字

json中的数字类型问题

JSON 协议中没有整型和浮点型之分,都是number。Go中json字符串中的数字经过json包反序列化会成为float64类型。如:

package main

import (
	"encoding/json"
	"fmt"
)

type Student struct {
	Name   string      `json:"name"`
	Age    interface{} `json:"age"`
	Weight float64     `json:"weight"`
	Height float64     `json:"height"`
}

func main() {

	s1 := Student{
		Name:   "jack",
		Age:    20,
		Weight: 61.5,
		Height: 172.5,
	}
	fmt.Printf("Before marshal/unmarshal, the type of age: %T\n", s1.Age)

	b, err := json.Marshal(s1)
	if err != nil {
		fmt.Printf("json.Marshal failed, err:%v\n", err)
		return
	}
	fmt.Printf("s1: %s\n", b)

	var s2 Student
	err = json.Unmarshal(b, &s2)
	if err != nil {
		fmt.Printf("json.Unmarshal failed, err:%v\n", err)
		return
	}
	fmt.Printf("s2: %#v\n", s2)
	fmt.Printf("After marshal/unmarshal, the type of age: %T\n", s2.Age)
}

输出:

Before marshal/unmarshal, the type of age: int
s1: {"name":"jack","age":20,"weight":61.5,"height":172.5}
s2: main.Student{Name:"jack", Age:20, Weight:61.5, Height:172.5}
After marshal/unmarshal, the type of age: float64

可见,int类型的age字段在经过序列化和反序列化之后变成了float64类型,要解决这个问题,需要使用decoder去反序列化。先得到json.Number类型,然后根据该字段的实际类型调用Float64()或Int64()方法。如:

package main

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

type Student struct {
	Name   string      `json:"name"`
	Age    interface{} `json:"age"`
	Weight float64     `json:"weight"`
	Height float64     `json:"height"`
}

func main() {

	s1 := Student{
		Name:   "jack",
		Age:    20,
		Weight: 61.5,
		Height: 172.5,
	}
	fmt.Printf("Before marshal/unmarshal, the type of age: %T\n", s1.Age)

	b, err := json.Marshal(s1)
	if err != nil {
		fmt.Printf("json.Marshal failed, err:%v\n", err)
		return
	}
	fmt.Printf("s1: %s\n", b)

	var s2 Student
	// err = json.Unmarshal(b, &s2)
	decoder := json.NewDecoder(bytes.NewReader(b))
	decoder.UseNumber()
	err = decoder.Decode(&s2)
	if err != nil {
		fmt.Printf("json.Unmarshal failed, err:%v\n", err)
		return
	}
	fmt.Printf("s2: %#v\n", s2)
	fmt.Printf("After marshal/unmarshal, the type of age: %T\n", s2.Age)

	s2.Age, err = s2.Age.(json.Number).Int64()
	if err != nil {
		fmt.Printf("parse to int64 failed, err:%v\n", err)
		return
	}
	fmt.Printf("s2: %#v\n", s2)
	fmt.Printf("After marshal/unmarshal/parse, the type of age: %T\n", s2.Age)
}

输出:

Before marshal/unmarshal, the type of age: int
s1: {"name":"jack","age":20,"weight":61.5,"height":172.5}
s2: main.Student{Name:"jack", Age:"20", Weight:61.5, Height:172.5}
After marshal/unmarshal, the type of age: json.Number
s2: main.Student{Name:"jack", Age:20, Weight:61.5, Height:172.5}
After marshal/unmarshal/parse, the type of age: int64

反序列化字符串格式的数字

结构体字段是数字类型,而后端接收到的json串中,数字类型的字段值往往是字符串形式,这样在直接反序列化时会报错:

json.Unmarshal failed, err:json: cannot unmarshal string into Go struct field Student.age of type int64

解决这个问题,需要在json tag中的fieldName后面加上“string”。如下:

package main

import (
	"encoding/json"
	"fmt"
)

type Student struct {
	Name   string  `json:"name"`
	Age    int64   `json:"age,string"`
	Weight float64 `json:"weight,string"`
	Height float64 `json:"height,string"`
}

func main() {

	var s2 Student

	s1 := `{"name":"jack","age":"20","weight":"61.5","height":"172.5"}`
	err := json.Unmarshal([]byte(s1), &s2)
	if err != nil {
		fmt.Printf("json.Unmarshal failed, err:%v\n", err)
		return
	}
	fmt.Printf("s2: %#v\n", s2)
}

输出:

s2: main.Student{Name:"jack", Age:20, Weight:61.5, Height:172.5}

你可能感兴趣的:(golang)