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 convert json to struct in golang

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

这期内容当中小编将会给大家带来有关golang中怎么将json转化成struct,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

举个例子:

[{ "description" : "An imaginary server config file", "logs" : {"level":"verbose", "dir":"/var/log"}, "host" : "antlr.org", "admin": ["parrt", "tombu"], "aliases": [ ], "Spaces": { }, "keys":[1,2,3,4], "null_": null, "bool_": true, "structs":[{"a":"a","b":"2"}]}]

针对这个json文件可以生成如下数据结构

package Generated

type AutoGenerated []struct { Description string `json:"description"` Logs struct { Level string `json:"level"` Dir string `json:"dir"` } `json:"logs"` Host string `json:"host"` Admin []string `json:"admin"` Aliases []interface{} `json:"aliases"` Spaces struct { } `json:"Spaces"` Keys []float64 `json:"keys"` Null_ interface{} `json:"null_"` Bool_ bool `json:"bool_"` Structs []struct { A string `json:"a"` B string `json:"b"` } `json:"structs"`}

这个轮子的代码我放在

https://github.com/xiazemin/jsonToAll

欢迎体验

如何使用呢

package main

import ( "fmt" "github.com/xiazemin/jsonToAll/file" "github.com/xiazemin/jsonToAll/generator"

"log")

func main() { strOri,strGen:=generator.Gen("./t.json","go") fmt.Println(strOri) log.Println(strGen)

file.PutGoLang("./gen/t.go",strGen) strJsonOri,strJsonGen:=generator.Gen("./t.json","json") fmt.Println(strJsonGen) log.Println(strJsonOri)

file.PutJson("./gen/t.json",strJsonGen)}

上述就是小编为大家分享的golang中怎么将json转化成struct了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

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

Internet Technology

Wechat

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

12
Report