go语言通过结构体生成json示例解析
作者:Jeff的技术栈 时间:2024-04-26 17:23:53
通过结构体生成json
buf, err := json.MarshalIndent(s, "", " ") //格式化编码
package main
import (
"encoding/json"
"fmt"
)
//成员变量名首字母必须大写
type IT struct {
Company string `json:"-"` //此字段不会输出到屏幕
Subjects []string `json:"subjects"` //二次编码
IsOk bool `json:",string"`
Price float64 `json:",string"`
}
func main() {
//定义一个结构体变量,同时初始化
s := IT{"itcast", []string{"Go", "C++", "Python", "Test"}, true, 666.666}
//编码,根据内容生成json文本
//{"Company":"itcast","Subjects":["Go","C++","Python","Test"],"IsOk":true,"Price":666.666}
//buf, err := json.Marshal(s)
buf, err := json.MarshalIndent(s, "", "") //格式化编码
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Println(string(buf))
}
通过map生成json
package main
import (
"encoding/json"
"fmt"
)
func main() {
//创建一个map
m := make(map[string]interface{}, 4)
m["company"] = "itcast"
m["subjects"] = []string{"Go", "C++", "Python", "Test"}
m["isok"] = true
m["price"] = 666.666
//编码成json
//result, err := json.Marshal(m)
result, err := json.MarshalIndent(m, "", "")
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Println("result = ", string(result))
}
json解析到结构体
err := json.Unmarshal([]byte(jsonBuf), &tmp) //第二个参数要地址传递
package main
import (
"encoding/json"
"fmt"
)
type IT struct {
Company string `json:"company"`
Subjects []string `json:"subjects"` //二次编码
IsOk bool `json:"isok"`
Price float64 `json:"price"`
}
func main() {
jsonBuf := `
{
"company": "itcast",
"subjects": [
"Go",
"C++",
"Python",
"Test"
],
"isok": true,
"price": 666.666
}`
var tmp IT //定义一个结构体变量
err := json.Unmarshal([]byte(jsonBuf), &tmp) //第二个参数要地址传递
if err != nil {
fmt.Println("err = ", err)
return
}
//fmt.Println("tmp = ", tmp)
fmt.Printf("tmp = %+v\n", tmp)
type IT2 struct {
Subjects []string `json:"subjects"` //二次编码
}
var tmp2 IT2
err = json.Unmarshal([]byte(jsonBuf), &tmp2) //第二个参数要地址传递
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Printf("tmp2 = %+v\n", tmp2)
}
json解析到map
package main
import (
"encoding/json"
"fmt"
)
func main() {
jsonBuf := `
{
"company": "itcast",
"subjects": [
"Go",
"C++",
"Python",
"Test"
],
"isok": true,
"price": 666.666
}`
//创建一个map
m := make(map[string]interface{}, 4)
err := json.Unmarshal([]byte(jsonBuf), &m) //第二个参数要地址传递
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Printf("m = %+v\n", m)
//var str string
//str = string(m["company"]) //err, 无法转换
//fmt.Println("str = ", str)
var str string
//类型断言, 值,它是value类型
for key, value := range m {
//fmt.Printf("%v ============> %v\n", key, value)
switch data := value.(type) {
case string:
str = data
fmt.Printf("map[%s]的值类型为string, value = %s\n", key, str)
case bool:
fmt.Printf("map[%s]的值类型为bool, value = %v\n", key, data)
case float64:
fmt.Printf("map[%s]的值类型为float64, value = %f\n", key, data)
case []string:
fmt.Printf("map[%s]的值类型为[]string, value = %v\n", key, data)
case []interface{}:
fmt.Printf("map[%s]的值类型为[]interface, value = %v\n", key, data)
}
}
}
来源:https://www.cnblogs.com/guyouyin123/p/13985098.html
标签:go,结构体,json
0
投稿
猜你喜欢
python递归法实现简易连连看小游戏
2023-04-18 12:41:02
python获取对象信息的实例详解
2022-04-30 14:55:50
ASP FCKeditor在线编辑器使用方法
2023-01-12 23:15:04
python爬虫 正则表达式解析
2022-07-16 18:24:01
详细说明关于Java的数据库连接(JDBC)
2024-01-18 09:00:16
JavaScript用Number方法实现string转int
2024-05-11 09:33:43
python如何通过闭包实现计算器的功能
2023-10-04 06:08:06
Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来
2021-09-26 00:05:53
Python随机函数库random的使用方法详解
2021-06-07 16:16:23
一道求$b相对于$a的相对路径的php代码
2023-07-12 05:37:46
python 爬取影视网站下载链接
2022-07-30 00:59:42
Golang中切片的用法与本质详解
2024-04-28 10:47:11
JavaScript实现QQ聊天室功能
2024-04-19 09:47:53
Python初学者必备的文件读写指南
2023-03-16 11:44:15
详解Python的Django框架中的模版相关知识
2023-04-22 02:46:25
python中为main方法传参问题
2023-05-20 04:48:12
在Python中使用Neo4j的方法
2023-01-16 04:12:39
Python EOL while scanning string literal问题解决方法
2021-01-20 23:58:38
Python pickle模块用法实例
2022-06-30 10:29:04
MySQL窗口函数的具体使用
2024-01-20 07:55:27