Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse json strings with Unmarshal in Go language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report