Christian-health / go-learn

0 stars 0 forks source link

Go的json解析:Marshal与Unmarshal #24

Open Christian-health opened 5 years ago

Christian-health commented 5 years ago

https://blog.csdn.net/zxy_666/article/details/80173288

image

Christian-health commented 5 years ago
package main
import (
   "encoding/json"
   "fmt"
   "reflect"
)
type StuRead struct {
    Name  interface{} `json:"name"`
    Age   interface{}
    HIgh  interface{}
    sex   interface{}
    Class Class `json:"class"` //注意这里的类型是Class
    Test  interface{}
}

type Class struct {
    Name  string
    Grade int
}

func main() {
    //json字符中的"引号,需用\进行转义,否则编译出错
    //json字符串沿用上面的结果,但对key进行了大小的修改,并添加了sex数据
    data:="{\"name\":\"张三\",\"Age\":18,\"high\":true,\"sex\":\"男\",\"CLASS\":{\"naME\":\"1班\",\"GradE\":3}}"
    str:=[]byte(data)

    //1.Unmarshal的第一个参数是json字符串,第二个参数是接受json解析的数据结构。
    //第二个参数必须是指针,否则无法接收解析的数据,如stu仍为空对象StuRead{}
    //2.可以直接stu:=new(StuRead),此时的stu自身就是指针
    stu:=StuRead{}
    fmt.Println(stu)
    //与前边json解析的代码一致
    PrintType(&stu) //打印json解析前变量类型
    json.Unmarshal(str,&stu)
    fmt.Println("--------------json 解析后-----------")
    PrintType(&stu) //打印json解析后变量类型

}

func PrintType(stu *StuRead){
    nameType:=reflect.TypeOf(stu.Name)
    ageType:=reflect.TypeOf(stu.Age)
    highType:=reflect.TypeOf(stu.HIgh)
    sexType:=reflect.TypeOf(stu.sex)
    classType:=reflect.TypeOf(stu.Class)
    testType:=reflect.TypeOf(stu.Test)

    fmt.Println("nameType:",nameType)
    fmt.Println("ageType:",ageType)
    fmt.Println("highType:",highType)
    fmt.Println("sexType:",sexType)
    fmt.Println("classType:",classType)
    fmt.Println("testType:",testType)
}

结果

-> # go  run json.go
{<nil> <nil> <nil> <nil> { 0} <nil>}
nameType: <nil>
ageType: <nil>
highType: <nil>
sexType: <nil>
classType: main.Class  //注意这里的类型是main.Class  所以就是main包中的Class类型
testType: <nil>
--------------json 解析后-----------
nameType: string
ageType: float64
highType: bool
sexType: <nil>
classType: main.Class
testType: <nil>
root@ZTE [11:23:08 AM] [/opt/goyard]
-> #
Christian-health commented 5 years ago
package main
import (
   "encoding/json"
   "fmt"
   "reflect"
)
type StuRead struct {
    Name  interface{} `json:"name"`
    Age   interface{}
    HIgh  interface{}
    sex   interface{}
    Class interface{} `json:"class"`   //注意这里是interface{}类型
    Test  interface{}
}

type Class struct {
    Name  string
    Grade int
}

func main() {
    //json字符中的"引号,需用\进行转义,否则编译出错
    //json字符串沿用上面的结果,但对key进行了大小的修改,并添加了sex数据
    data:="{\"name\":\"张三\",\"Age\":18,\"high\":true,\"sex\":\"男\",\"CLASS\":{\"naME\":\"1班\",\"GradE\":3}}"
    str:=[]byte(data)

    //1.Unmarshal的第一个参数是json字符串,第二个参数是接受json解析的数据结构。
    //第二个参数必须是指针,否则无法接收解析的数据,如stu仍为空对象StuRead{}
    //2.可以直接stu:=new(StuRead),此时的stu自身就是指针
    stu:=StuRead{}
    fmt.Println(stu)
    //与前边json解析的代码一致
    PrintType(&stu) //打印json解析前变量类型
    json.Unmarshal(str,&stu)
    fmt.Println("--------------json 解析后-----------")
    PrintType(&stu) //打印json解析后变量类型

}

func PrintType(stu *StuRead){
    nameType:=reflect.TypeOf(stu.Name)
    ageType:=reflect.TypeOf(stu.Age)
    highType:=reflect.TypeOf(stu.HIgh)
    sexType:=reflect.TypeOf(stu.sex)
    classType:=reflect.TypeOf(stu.Class)
    testType:=reflect.TypeOf(stu.Test)

    fmt.Println("nameType:",nameType)
    fmt.Println("ageType:",ageType)
    fmt.Println("highType:",highType)
    fmt.Println("sexType:",sexType)
    fmt.Println("classType:",classType)
    fmt.Println("testType:",testType)
}

结果

-> # go run  json.go
{<nil> <nil> <nil> <nil> <nil> <nil>}
nameType: <nil>
ageType: <nil>
highType: <nil>
sexType: <nil>
classType: <nil>
testType: <nil>
--------------json 解析后-----------
nameType: string
ageType: float64
highType: bool
sexType: <nil>
classType: map[string]interface {}
testType: <nil>
root@ZTE [11:20:54 AM] [/opt/goyard]
Christian-health commented 5 years ago

这个没有看 挺好的 https://www.cnblogs.com/fengbohello/p/4665883.html

image