首頁 後端開發 Golang Go語言中處理json資料的方法

Go語言中處理json資料的方法

Jan 11, 2020 pm 05:14 PM
go語言

Go語言中處理json資料的方法

Go json套件

#Marshal():Go資料物件-> json資料

UnMarshal():Json資料-> Go資料物件

func Marshal(v interface{}) ([]byte, error)
func Unmarshal(data []byte, v interface{}) error
登入後複製

#建構json資料

Marshal()和MarshalIndent()函數可以將資料封裝成json資料。

1、struct、slice、array、map都可以轉換成json

2、struct轉換成json的時候,只有字段首字母大寫的才會被轉換

3、map轉換的時候,key必須為string

4、封裝的時候,如果是指針,會追蹤指針指向的物件進行封裝

例如:

有一個struct結構:

type Post struct {
    Id      int
    Content string
    Author  string
}
登入後複製

這個結構表示部落格文章類型,有文章ID,文章內容,文章的提交作者。這沒什麼好說的,唯一需要指明的是:它是一個struct,struct可以封裝(編碼)成JSON資料。

要將這段struct資料轉換成json,只要使用Marshal()即可。如下:

post := &Post{1, "Hello World", "userA"}
b, err := json.Marshal(post)
if err != nil {
    fmt.Println(nil)
}
登入後複製

Marshal()返回的是一個[]byte類型,現在變數b已經儲存了一段[]byte類型的json數據,可以輸出它:

fmt.Println(string(b))
登入後複製

結果:

{"Id":1,"Content":"Hello World","Author":"userA"}
登入後複製

可以在封裝成json的時候進行"美化",使用MarshalIndent()即可自動添加前綴(前綴字串一般設定為空)和縮排:

c,err := json.MarshalIndent(post,"","\t")
if err != nil {
    fmt.Println(nil)
}
fmt.Println(string(c))
登入後複製

結果:

{
    "Id": 1,
    "Content": "Hello World",
    "Author": "userA"
}
登入後複製

除了struct,array、slice、map結構都能解析成json,但是map解析成json的時候,key必須只能是string,這是json語法要求的。

例如:

// slice -> json
s := []string{"a", "b", "c"}
d, _ := json.MarshalIndent(s, "", "\t")
fmt.Println(string(d))

// map -> json
m := map[string]string{
    "a":"aa",
    "b":"bb",
    "c":"cc",
}
e,_ := json.MarshalIndent(m,"","\t")
fmt.Println(string(e))
登入後複製

返回結果:

[
    "a",
    "b",
    "c"
]
{
    "a": "aa",
    "b": "bb",
    "c": "cc"
}
登入後複製

#使用struct tag輔助建構json

#struct能被轉換的字段都是首字母大寫的字段,但如果想要在json中使用小寫字母開頭的key,可以使用struct的tag來輔助反射。

例如,Post結構增加一個首字母小寫的字段createAt。

type Post struct {
    Id      int      `json:"ID"`
    Content string   `json:"content"`
    Author  string   `json:"author"`
    Label   []string `json:"label"`
}


postp := &Post{
    2,
    "Hello World",
    "userB",
    []string{"linux", "shell"},
    }

p, _ := json.MarshalIndent(postp, "", "\t")
fmt.Println(string(p))
登入後複製

結果:

{
    "ID": 2,
    "content": "Hello World",
    "author": "userB",
    "label": [
        "linux",
        "shell"
    ]
}
登入後複製

使用struct tag的時候,幾個注意點:

1、tag中標識的名稱將稱為json資料中key的值

2、tag可以設定為`json:"-"`來表示本字段不轉換為json數據,即使這個字段名首字母大寫

如果想要json key的名稱為字符"-",則可以特殊處理`json:"-,"`,也就是加上一個逗號

3、如果tag中帶有,omitempty選項,那麼如果這個字段的值為0值,即false、0、""、nil等,這個字段將不會轉換到json中

4、如果字段的類型為bool、string、int類、float類,而tag中又帶有, string選項,那麼這個欄位的值將會轉換成json字串

例如:

type Post struct {
    Id      int      `json:"ID,string"`
    Content string   `json:"content"`
    Author  string   `json:"author"`
    Label   []string `json:"label,omitempty"`
}
登入後複製

解析json資料到struct(結構已知)

json資料可以解析到struct或空介面interface{}中(也可以是slice、map等)。理解了上面建構json時的tag規則,理解解析json就很簡單了。

例如,以下是一段json資料:

{
    "id": 1,
    "content": "hello world",
    "author": {
        "id": 2,
        "name": "userA"
    },
    "published": true,
    "label": [],
    "nextPost": null,
    "comments": [{
            "id": 3,
            "content": "good post1",
            "author": "userB"
        },
        {
            "id": 4,
            "content": "good post2",
            "author": "userC"
        }
    ]
}
登入後複製

分析下這段json資料:

1、頂層的大括號表示是匿名對象,映射到Go中是一個struct,假設這個struct名稱為Post

2、頂層大括號裡的都是Post結構中的字段,這些字段因為都是json數據,所以必須都首字母大寫,同時設定tag, tag中的名稱小寫

3、其中author是一個子對象,映射到Go中是另一個struct,在Post中這個欄位的名稱為Author,假設名稱和struct名稱相同,也為Author

4、label是數組,映射到Go中可以是slice,也可以是array,且因為json array為空,所以Go中的slice/array類型不定,例如可以是int,可以是string ,也可以是interface{},對於這裡的範例來說,我們知道標籤肯定是string

5、nextPost是一個子對象,映射到Go中是一個struct,但因為json中這個物件為null,表示這個物件不存在,所以無法判斷映射到Go中struct的型別。但對此處的範例來說,是沒有下一篇文章,所以它的類型應該也是Post類型

6、comment是子對象,且是數組包圍的,映射到Go中,是一個slice/array,slice/array的型別是struct

分析之後,對應地去建構struct和struct的tag就很容易了。如下,是根據上面分析構建出來的數據結構:

type Post struct {
    ID        int64         `json:"id"`       
    Content   string        `json:"content"`  
    Author    Author        `json:"author"`   
    Published bool          `json:"published"`
    Label     []string      `json:"label"`    
    NextPost  *Post         `json:"nextPost"` 
    Comments  []*Comment    `json:"comments"` 
}

type Author struct {
    ID   int64  `json:"id"`  
    Name string `json:"name"`
}

type Comment struct {
    ID      int64  `json:"id"`     
    Content string `json:"content"`
    Author  string `json:"author"` 
}
登入後複製

注意,前面在介紹構建json數據的時候說明過,指針會進行追踪,所以這裡反推出來的struct中使用指針類型是沒問題的。

於是,解析上面的json資料到Post類型的物件中,假設這個json資料存放在a.json檔案中。程式碼如下:

func main() {
    // 打开json文件
    fh, err := os.Open("a.json")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer fh.Close()
    // 读取json文件,保存到jsonData中
    jsonData, err := ioutil.ReadAll(fh)
    if err != nil {
        fmt.Println(err)
        return
    }
    
    var post Post
    // 解析json数据到post中
    err = json.Unmarshal(jsonData, &post)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(post)
}
登入後複製

輸出結果:

{1 hello world {2 userA} true [] <nil> [0xc042072300 0xc0420723c0]}
登入後複製

也许你已经感受到了,从json数据反推算struct到底有多复杂,虽然逻辑不难,但如果数据复杂一点,这是件非常恶心的事情。所以,使用别人写好的工具来自动转换吧。本文后面有推荐json到数据结构的自动转换工具。

解析json到interface(结构未知)

上面是已知json数据结构的解析方式,如果json结构是未知的或者结构可能会发生改变的情况,则解析到struct是不合理的。这时可以解析到空接口interface{}或map[string]interface{}类型上,这两种类型的结果是完全一致的。

解析到interface{}上时,Go类型和JSON类型的对应关系如下

  JSON类型             Go类型                
---------------------------------------------
JSON objects    <-->  map[string]interface{} 
JSON arrays     <-->  []interface{}          
JSON booleans   <-->  bool                   
JSON numbers    <-->  float64                
JSON strings    <-->  string                 
JSON null       <-->  nil
登入後複製

例如:

func main() {
    // 读取json文件
    fh, err := os.Open("a.json")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer fh.Close()
    jsonData, err := ioutil.ReadAll(fh)
    if err != nil {
        fmt.Println(err)
        return
    }
    
    // 定义空接口接收解析后的json数据
    var unknown interface{}
    // 或:map[string]interface{} 结果是完全一样的
    err = json.Unmarshal(jsonData, &unknown)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(unknown)
}
登入後複製

输出结果:

map[nextPost:<nil> comments:[map[id:3 content:good post1
author:userB] map[id:4 content:good post2 author:userC]]
id:1 content:hello world author:map[id:2 name:userA] published:true label:[]]
登入後複製

上面将输出map结构。这是显然的,因为类型对应关系中已经说明了,json object解析到Go interface的时候,对应的是map结构。如果将上面输出的结构进行一下格式化,得到的将是类似下面的结构:

map[
    nextPost:<nil>
    comments:[
        map[
            id:3
            content:good post1
            author:userB
        ]
        map[
            id:4
            content:good post2
            author:userC
        ]
    ]
    id:1
    content:hello world
    author:map[
        id:2
        name:userA
    ]
    published:true
    label:[]
]
登入後複製

现在,可以从这个map中去判断类型、取得对应的值。但是如何判断类型?可以使用类型断言:

func main() {
    // 读取json数据
    fh, err := os.Open("a.json")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer fh.Close()
    jsonData, err := ioutil.ReadAll(fh)
    if err != nil {
        fmt.Println(err)
        return
    }
    
    // 解析json数据到interface{}
    var unknown interface{}
    err = json.Unmarshal(jsonData, &unknown)
    if err != nil {
        fmt.Println(err)
        return
    }

    // 进行断言,并switch匹配
    m := unknown.(map[string]interface{})
    for k, v := range m {
        switch vv := v.(type) {
        case string:
            fmt.Println(k, "type: string\nvalue: ", vv)
            fmt.Println("------------------")
        case float64:
            fmt.Println(k, "type: float64\nvalue: ", vv)
            fmt.Println("------------------")
        case bool:
            fmt.Println(k, "type: bool\nvalue: ", vv)
            fmt.Println("------------------")
        case map[string]interface{}:
            fmt.Println(k, "type: map[string]interface{}\nvalue: ", vv)
            for i, j := range vv {
                fmt.Println(i,": ",j)
            }
            fmt.Println("------------------")
        case []interface{}:
            fmt.Println(k, "type: []interface{}\nvalue: ", vv)
            for key, value := range vv {
                fmt.Println(key, ": ", value)
            }
            fmt.Println("------------------")
        default:
            fmt.Println(k, "type: nil\nvalue: ", vv)
            fmt.Println("------------------")
        }
    }
}
登入後複製

结果如下:

comments type: []interface{}
value:  [map[id:3 content:good post1 author:userB] map[author:userC id:4 content:good post2]]
0 :  map[id:3 content:good post1 author:userB]
1 :  map[id:4 content:good post2 author:userC]
------------------
id type: float64
value:  1
------------------
content type: string
value:  hello world
------------------
author type: map[string]interface{}
value:  map[id:2 name:userA]
name :  userA
id :  2
------------------
published type: bool
value:  true
------------------
label type: []interface{}
value:  []
------------------
nextPost type: nil
value:  <nil>
------------------
登入後複製

可见,从interface中解析非常复杂,而且可能因为嵌套结构而导致无法正确迭代遍历。这时候,可以使用第三方包simplejson,见后文。

解析、创建json流

除了可以直接解析、创建json数据,还可以处理流式数据。

1、type Decoder解码json到Go数据结构

2、ype Encoder编码Go数据结构到json

例如:

const jsonStream = `
    {"Name": "Ed", "Text": "Knock knock."}
    {"Name": "Sam", "Text": "Who&#39;s there?"}
    {"Name": "Ed", "Text": "Go fmt."}
    {"Name": "Sam", "Text": "Go fmt who?"}
    {"Name": "Ed", "Text": "Go fmt yourself!"}
`
type Message struct {
    Name, Text string
}
dec := json.NewDecoder(strings.NewReader(jsonStream))
for {
    var m Message
    if err := dec.Decode(&m); err == io.EOF {
        break
    } else if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s: %s\n", m.Name, m.Text)
}
登入後複製

输出:

Ed: Knock knock.
Sam: Who&#39;s there?
Ed: Go fmt.
Sam: Go fmt who?
Ed: Go fmt yourself!
登入後複製

再例如,从标准输入读json数据,解码后删除名为Name的元素,最后重新编码后输出到标准输出。

func main() {
    dec := json.NewDecoder(os.Stdin)
    enc := json.NewEncoder(os.Stdout)
    for {
        var v map[string]interface{}
        if err := dec.Decode(&v); err != nil {
            log.Println(err)
            return
        }
        for k := range v {
            if k != "Name" {
                delete(v, k)
            }
        }
        if err := enc.Encode(&v); err != nil {
            log.Println(err)
        }
    }
}
登入後複製

更多go语言知识请关注PHP中文网go语言教程栏目。

以上是Go語言中處理json資料的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Go語言中用於浮點數運算的庫有哪些? Go語言中用於浮點數運算的庫有哪些? Apr 02, 2025 pm 02:06 PM

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go的爬蟲Colly中Queue線程的問題是什麼? Go的爬蟲Colly中Queue線程的問題是什麼? Apr 02, 2025 pm 02:09 PM

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Apr 02, 2025 pm 04:12 PM

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Go語言中`var`和`type`關鍵字定義結構體的區別是什麼? Go語言中`var`和`type`關鍵字定義結構體的區別是什麼? Apr 02, 2025 pm 12:57 PM

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...

在Go語言中使用Redis Stream實現消息隊列時,如何解決user_id類型轉換問題? 在Go語言中使用Redis Stream實現消息隊列時,如何解決user_id類型轉換問題? Apr 02, 2025 pm 04:54 PM

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

在 Go 語言中,為什麼使用 Println 和 string() 函數打印字符串會出現不同的效果? 在 Go 語言中,為什麼使用 Println 和 string() 函數打印字符串會出現不同的效果? Apr 02, 2025 pm 02:03 PM

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

在使用Go語言和viper庫時,為什麼傳遞指針的指針是必要的? 在使用Go語言和viper庫時,為什麼傳遞指針的指針是必要的? Apr 02, 2025 pm 04:00 PM

Go指針語法及viper庫使用中的尋址問題在使用Go語言進行編程時,理解指針的語法和使用方法至關重要,尤其是在...

為什麼Go語言中使用for range遍歷slice並存入map時,所有值會變成最後一個元素? 為什麼Go語言中使用for range遍歷slice並存入map時,所有值會變成最後一個元素? Apr 02, 2025 pm 04:09 PM

為什麼Go語言中的map迭代會導致所有值變成最後一個元素?在Go語言中,面對一些面試題時,經常會遇到關於map�...

See all articles