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

type SampleJson struct { //json结构体
First interface{} `json:"first"`
Second interface{} `json:"second"`
Third interface{} `json:"third"`
}

func (p *SampleJson) Format() { //类型转换
elem := reflect.ValueOf(p).Elem()
for i := 0; i < elem.NumField(); i++ {
v := 0
switch elem.Field(i).Interface().(type) {
case string:
n, _ := strconv.ParseFloat(elem.Field(i).Interface().(string), 64)
v = int(n)
case float64:
v = int(elem.Field(i).Interface().(float64))
default:
}
elem.Field(i).Set(reflect.ValueOf(v)) //转换后重新修改值
}
}

func main() {
s := `{"first":"1","second":2,"third":"3.33"}`
p := &SampleJson{}
json.Unmarshal([]byte(s), p)
p.Format() //转换
fmt.Println(p)
b, _ := json.Marshal(p)
fmt.Println(string(b))
n := p.First.(int)
fmt.Println(n)
}