Golang 提供了多个 API 来处理 JSON,包括使用 encoding/json 包来往来于内置和自定义数据类型。

Golang 使用 Marshal() 和 Unmarshal() 方法将对象转换为 JSON 和 JSON 数据为字符串。 这些方法以字节格式返回数据,我们需要将返回的数据更改为 JSON 或 String。
在本文中,我们将了解 Golang 中的数据编组和解组。
Golang 中的元帅
将 Go 对象转换为 JSON 在 Golang 中称为编组,使用内置的 encoding/json 包来执行 JSON 相关的操作。
json.Marshal(v any) ([]byte, error)
借助上述函数,Marshal 返回 v 的 JSON 编码,进一步,我们可以将字节数据转换为字符串并返回。
package main
import (
"encoding/json"
"fmt"
)
type Books struct {
Name string
Author string
Rating float64
}
func main() {
book := Books{Name: "Rich Dad Poor Dad", Author: "Robert Kiyosaki and Sharon Lechter", Rating: 4.7}
bookData, err := json.Marshal(book)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(bookData))
// Output: {"Name":"Rich Dad Poor Dad","Author":"Robert Kiyosaki and Sharon Lechter","Rating":4.7}
}
在 Golang 中解组
这与元帅正好相反。 将 JSON(Byte data) 转换为 Struct 并将结果存储在变量指向的值中。
json.Unmarshal(data []byte, v any)
借助上述函数,Unmarshal 返回 JSON 数据的实际值。
package main
import (
"encoding/json"
"fmt"
)
type Book struct {
Name string `json:"name"`
Author string `json:"author"`
Rating float64 `json:"rating"`
}
func main() {
bookJsonData := `{"Name":"Hamlet", "Author":"William Shakespeare", "Rating":4.3}`
bookBytes := []byte(bookJsonData)
var book Book
json.Unmarshal(bookBytes, &book)
fmt.Println(book.Name, "," , book.Author, "," ,book.Rating)
//Output: Hamlet , William Shakespeare , 4.3
}
目前让我们看看使用上述函数编写一些代码并对 JSON 数据集进行一些修改。
让我们思考给定的数据集:
{
"_id": "62b435e407bc586da8d297cb",
"index": 4,
"guid": "57a22da3-d791-4420-9245-465a34730537",
"gender": "male",
"company": "EZENT",
"email": "barrerajarvis@ezent.com",
"phone": "+1 (801) 499-2594",
"friends": [
{
"id": 0,
"name": "Dale Sexton",
"email": "Dale.sexton@example.com"
},
{
"id": 1,
"name": "Watkins Puckett",
"email": "Watkins.puckett@example.com"
},
{
"id": 2,
"name": "Luann Bond",
"email": "Luann.bond@example.com"
}
],
"greeting": "Hello, Barrera Jarvis! You have 8 unread messages.",
"favoriteFruit": "banana"
}
并且,问题陈述是写下从朋友数组中删除电子邮件字段的代码。
目前,让我们写下修改 JSON 数据的代码:
package main
import (
"encoding/json"
"log"
"os"
)
func main() {
inputs := `{
"_id": "62b435e407bc586da8d297cb",
"index": 4,
"guid": "57a22da3-d791-4420-9245-465a34730537",
"gender": "male",
"company": "EZENT",
"email": "barrerajarvis@ezent.com",
"phone": "+1 (801) 499-2594",
"friends": [
{
"id": 0,
"name": "Dale Sexton",
"email": "Dale.sexton@example.com"
},
{
"id": 1,
"name": "Watkins Puckett",
"email": "Watkins.puckett@example.com"
},
{
"id": 2,
"name": "Luann Bond",
"email": "Luann.bond@example.com"
}
],
"greeting": "Hello, Barrera Jarvis! You have 8 unread messages.",
"favoriteFruit": "banana"
}`
var v any
if err := json.Unmarshal([]byte(inputs), &v); err != nil {
log.Fatal(err)
}
if v, ok := v.(map[string]any); ok {
// if the "inputs" field is a JSON array ...
if v, ok := v["friends"].([]any); ok {
// for each element in the JSON array ...
for _, v := range v {
// if the element is a JSON object ...
if v, ok := v.(map[string]any); ok {
// remove key boo
delete(v, "email")
}
}
}
}
p, _ := json.Marshal(v)
os.Stdout.Write(p)
//Output : {"_id":"62b435e407bc586da8d297cb","company":"EZENT","email":"barrerajarvis@ezent.com","favoriteFruit":"banana","friends":[{"id":0,"name":"Dale Sexton"},
// {"id":1,"name":"Watkins Puckett"},{"id":2,"name":"Luann Bond"}], "gender":"male","greeting":"Hello, Barrera Jarvis! You have 8 unread messages.",
// "guid":"57a22da3-d791-4420-9245-465a34730537","index":4,"phone":"+1 (801) 499-2594"}
}
我希望你喜爱这篇文章。
谢谢阅读
关注七爪网,获取更多APP/小程序/网站源码资源!
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END



















暂无评论内容