当前位置: 首页 > 工具软件 > Argo JSON > 使用案例 >

Go语言的处理JSON

宰父劲
2023-12-01

JavaScript对象表示法(JavaScript Object Notation,JSON)是一种用于存储和交换数据的格式。

Go语言非常适合用来创建收发JSON的客户端和服务器。

创建结构体,程序清单如下:

package main

import (
	"fmt"
)
type Person struct{
  Name string
  Age int
  Hobbies []string
}
func main() {
	hobbies:=[]string{"Cycling","Cheese","Techno"}
  p:=Person{
    Name:"George",
    Age:40,
    Hobbies:hobbies,
  }
  fmt.Printf("%+v\n",p)
}

运行结果如下:

{Name:George Age:40 Hobbies:[Cycling Cheese Techno]}

将结构体编码为JSON格式:

package main

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

type Person struct {
	Name    string
	Age     int
	Hobbies []string
}

func main() {
	hobbies := []string{"Cycling", "Cheese", "Techno"}
	p := Person{
		Name:    "George",
		Age:     40,
		Hobbies: hobbies,
	}
	fmt.Printf("%+v\n", p)
	jsonByteData, err := json.Marshal(p)
	if err != nil {
		log.Fatal(err)
	}
	jsonStringData := string(jsonByteData)
	fmt.Println(jsonStringData)
}

运行结果如下:

{Name:George Age:40 Hobbies:[Cycling Cheese Techno]}
{"Name":"George","Age":40,"Hobbies":["Cycling","Cheese","Techno"]}

使用结构体标签,程序清单如下:

package main

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

type Person struct {
	Name    string   `json:"name"`
	Age     int      `json:"age"`
	Hobbies []string `json:"hobbies"`
}

func main() {
	hobbies := []string{"Cycling", "Cheese", "Techno"}
	p := Person{
		Name:    "George",
		Age:     40,
		Hobbies: hobbies,
	}
	fmt.Printf("%+v\n", p)
	jsonByteData, err := json.Marshal(p)
	if err != nil {
		log.Fatal(err)
	}
	jsonStringData := string(jsonByteData)
	fmt.Println(jsonStringData)
}

运行结果如下:

{Name:George Age:40 Hobbies:[Cycling Cheese Techno]}
{"name":"George","age":40,"hobbies":["Cycling","Cheese","Techno"]}

忽略空的结构体字段,程序清单如下:

package main

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

type Person struct {
	Name    string   `json:"name,omitempty"`
	Age     int      `json:"age,omitempty"`
	Hobbies []string `json:"hobbies,omitempty"`
}

func main() {
	hobbies := []string{"Cycling", "Cheese", "Techno"}
	p := Person{
		Name:    "George",
		Age:     40,
		Hobbies: hobbies,
	}
	fmt.Printf("%+v\n", p)
	jsonByteData, err := json.Marshal(p)
	if err != nil {
		log.Fatal(err)
	}
	jsonStringData := string(jsonByteData)
	fmt.Println(jsonStringData)
}

运行结果如下:书上说是{} ,打印出来的空JSON对象。搞不清楚。

{Name:George Age:40 Hobbies:[Cycling Cheese Techno]}
{"name":"George","age":40,"hobbies":["Cycling","Cheese","Techno"]}

解码JSON:

将JSON数据解码为结构体,程序清单如下:

package main

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

type Person struct {
	Name    string   `json:"name"`
	Age     int      `json:"age"`
	Hobbies []string `json:"hobbies"`
}

func main() {
	jsonStringData := `{"name":"George","age":40,"hobbies":["Cycling","Cheese","Techno"]}`
	jsonByteData := []byte(jsonStringData)
	p := Person{}
	err := json.Unmarshal(jsonByteData, &p)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", p)
}

运行结果如下:

{Name:George Age:40 Hobbies:[Cycling Cheese Techno]}

映射数据类型:

Go是一种强类型语言,而JavaScript是一种弱类型语言。

Go和JSON数据类型
JSONGo
Booleanbool
Numberfloat64
Stringstring
Array[ ]interface{}
Objectmap[string]interface{}
Nullnil

在JSON和Go之间映射数据类型,程序清单如下:

package main

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

type Switch struct {
	On bool `json:"on"`
}

func main() {
	jsonStringData := `{"on":"true"}`
	jsonByteData := []byte(jsonStringData)
	s := Switch{}
	err := json.Unmarshal(jsonByteData, &s)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", s)
}

运行结果如下:

2022/01/26 21:31:46 json: cannot unmarshal string into Go struct field Switch.on of type bool
exit status 1

处理通过HTTP收到的JSON:

通过HTTP获取JSON,程序清单如下:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

type User struct {
	Name string `json:"name"`
	Blog string `json:"blog"`
}

func main() {
	var u User
	res, err := http.Get("https://api.github.com/users/shapeshed")
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
	err = json.NewDecoder(res.Body).Decode(&u)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", u)
}

运行结果如下:

{Name: Blog:}

 类似资料: