JavaScript对象表示法(JSON)是一种用于发送和接收结构化信息的标准协议。

1. marshaling

将一个Go语言中的数据结构转为JSON的过程叫编组(marshaling)。编组通过调用json.Marshal函数完成:

package main

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

type Movie struct {
    Title  string
    Year   int  `json:"released"`
    Color  bool `json:"color,omitempty"`
    Actors []string
}

func main() {
    var movies = []Movie{
        {Title: "Casablanca", Year: 1942, Color: false,
            Actors: []string{"Humphrey Bogart", "Ingrid Bergman"}},
        {Title: "Cool Hand Luke", Year: 1967, Color: true,
            Actors: []string{"Paul Newman"}},
        {Title: "Bullitt", Year: 1968, Color: true,
            Actors: []string{"Steve McQueen", "Jacqueline Bisset"}},
    }

    data, err := json.Marshal(movies)
    if err != nil {
        log.Fatalf("JSON marshaling failed: %s", err)
    }
    fmt.Printf("%s\n", data)
}

输出结果:

[{"Title":"Casablanca","released":1942,"Actors":["Humphrey Bogart","Ingrid Bergman"]},{"Title":"Cool Hand Luke","released":1967,"color":true,"Actors":["Paul Newman"]},{"Title":"Bullitt","released":1968,"color":true,"Actors":["Steve McQueen","Jacqueline Bisset"]}]

这种紧凑的表示形式虽然包含了全部的信息,但是很难阅读。为了生成便于阅读的格式,另一个json.MarshalIndent函数将产生整齐缩进的输出。该函数有两个额外的字符串参数用于表示每一行输出的前缀每一个层级的缩进

// data, err := json.Marshal(movies)
data, err := json.MarshalIndent(movies, "", "    ")

输出结果:

[
    {
        "Title": "Casablanca",
        "released": 1942,
        "Actors": [
            "Humphrey Bogart",
            "Ingrid Bergman"
        ]
    },
    {
        "Title": "Cool Hand Luke",
        "released": 1967,
        "color": true,
        "Actors": [
            "Paul Newman"
        ]
    },
    {
        "Title": "Bullitt",
        "released": 1968,
        "color": true,
        "Actors": [
            "Steve McQueen",
            "Jacqueline Bisset"
        ]
    }
]

2. unmarshaling

编码的逆操作是解码,对应将JSON数据解码为Go语言的数据结构,Go语言中一般叫unmarshaling,通过json.Unmarshal函数完成。
通过定义合适的Go语言数据结构,我们可以选择性地解码JSON中感兴趣的成员。下面的代码将JSON格式的电影数据解码为一个结构体slice,结构体中只有Title成员。当Unmarshal函数调用返回,slice将被只含有Title信息的值填充,其它JSON成员将被忽略。

var titles []struct{ Title string }
if err := json.Unmarshal(data, &titles); err != nil {
    log.Fatalf("JSON unmarshaling failed: %s", err)
}
fmt.Printf("%s\n", titles)

输出结果:

[{Casablanca} {Cool Hand Luke} {Bullitt}]

3. Github API 数据检索例子

许多web服务都提供JSON接口,通过HTTP接口发送JSON格式请求并返回JSON格式的信息。为了说明这一点,我们通过Github的issue查询服务来演示类似的用法。

首先,我们要定义合适的类型和常量:

// GO111MODULE=on,$GOPATH/src/github.com/lamxops/github
package github

import "time"

const IssuesURL = "https://api.github.com/search/issues"

type IssuesSearchResult struct {
    TotalCount int `json:"total_count"`
    Items      []*Issue
}

type Issue struct {
    Number   int
    HTMLURL  string `json:"html_url"`
    Title    string
    State    string
    User     *User
    CreateAt time.Time `json:"created_at"`
    Body     string
}

type User struct {
    Login   string
    HTMLURL string `json:"html_url"`
}

SearchIssues函数发出一个HTTP请求,然后解码返回的JSON格式的结果。

// GO111MODULE=on,$GOPATH/src/github.com/lamxops/github
package github

import (
    "encoding/json"
    "fmt"
    "net/http"
    "net/url"
    "strings"
)

func SearchIssues(terms []string) (*IssuesSearchResult, error) {
    q := url.QueryEscape(strings.Join(terms, " "))
    resp, err := http.Get(IssuesURL + "?q=" + q)
    if err != nil {
        return nil, err
    }

    if resp.StatusCode != http.StatusOK {
        resp.Body.Close()
        return nil, fmt.Errorf("search query failed: %s", resp.Status)
    }

    var result IssuesSearchResult
    if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
        resp.Body.Close()
        return nil, err
    }

    resp.Body.Close()
    return &result, nil
}

打印issue:

// GO111MODULE=on,$GOPATH/src/github.com/gopl/chapter04
package main

import (
    "fmt"
    "log"
    "os"

    "github.com/lamxops/github"
)

func main() {
    result, err := githubutil.SearchIssues(os.Args[1:])
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%d issues:\n", result.TotalCount)
    for _, item := range result.Items {
        fmt.Printf("#%-5d %9.9s %.55s\n", item.Number, item.User.Login, item.Title)
    }
}

提示:由于采用GO111MODULE=on,在import "github.com/lamxops/github"的时候,需要在go.mod文件中添加如下内容:

require github.com/lamxops/github v0.0.0
replace github.com/lamxops/github => ../github

构建执行:

// cd $GOPATH/src/github.com/gopl/chapter04
go build .
./chapter04.exe repo:golang/go is:open json decoder