Get the App
SLTechnology News&Howtos  ›  Development  › 

How to parse json strings with Unmarshal in Go language

Shulou Source: shulou.com Published: 2022-05-31 17:25:19 09月20日 Update

This article mainly introduces the "Go language how to use Unmarshal to parse json strings" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "Go language how to use Unmarshal to parse json strings" article can help you solve the problem.

A simple parsing example:

First of all, let's take an example from the official documentation:

Package mainimport ("fmt"encoding/json") type Animal struct {Name string Order string} func main () {var jsonBlob = [] byte (`[{"Name": "Platypus", "Order": "Monotremata"}, {"Name": "Quoll", "Order": "Dasyuromorphia"}]`) var animals [] Animal err: = json.Unmarshal (jsonBlob) & animals) if err! = nil {fmt.Println ("error:", err)} fmt.Printf ("% + v", animals)}

Output:

[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]

Simply modify it as follows:

Package mainimport ("fmt"encoding/json") type Animal struct {Name string Order string} func main () {var jsonBlob = [] byte (`{"Name": "Platypus", "Order": "Monotremata"}`) var animals Animal err: = json.Unmarshal (jsonBlob, & animals) if err! = nil {fmt.Println ("error:", err)} fmt.Printf ("% + v", animals)}

Output:

{Name:Platypus Order:Monotremata}

Or the previous example:

Parse an json string like this:

{"first fruit": {"describe": "an apple", "icon": "appleIcon", "name": "apple"}, "second fruit": {"describe": "an orange", "icon": "orangeIcon", "name": "orange"}, "three fruit array": ["eat 0", "eat 1" "eat 2", "eat 3", "eat 4"]}

Go Code:

Package mainimport ("fmt"encoding/json") type Fruit struct {Describe string `json: "describe" `Icon string `json: "icon" `Name string `json: "name" `} type FruitGroup struct {FirstFruit * Fruit `json: "first fruit"` / / pointer to the reference object; if the pointer is not used, just copy the SecondFruit * Fruit `json: "second fruit" `/ / pointer to the reference object If you don't use a pointer Just copy THreeFruitArray [] string `json: "three fruit array" `} func main () {var jsonBlob = [] byte (` {"first fruit": {"describe": "an apple", "icon": "appleIcon", "name": "apple"}, "second fruit": {"describe": "an orange", "icon": "appleIcon") "name": "orange"}, "three fruit array": ["eat 0", "eat 1", "eat 2", "eat 3"]} `) var fruitGroup FruitGroup err: = json.Unmarshal (jsonBlob, & fruitGroup) if err! = nil {fmt.Println ("error:", err)} fmt.Printf ("% + v\ n" FruitGroup) fmt.Printf ("% + v\ n", fruitGroup.FirstFruit) fmt.Printf ("% + v\ n", fruitGroup.SecondFruit)}

Running result:

{FirstFruit:0xc00006c5a0 SecondFruit:0xc00006c5d0 THreeFruitArray: [eat 0 eat 1 eat 2 eat 3]}

& {Describe:an appleIcon: appleIcon Name:apple}

& {Describe:an orange Icon:appleIcon Name:orange}

This is the end of the introduction to "how the Go language uses Unmarshal to parse json strings". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Characters strings pointers languages examples knowledge no just objects points industries output different practical code content official practical practical documentation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi NVidia Huawei Shulou Technology