博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go_json解析
阅读量:4340 次
发布时间:2019-06-07

本文共 3154 字,大约阅读时间需要 10 分钟。

 

 总结:

 其他类型转json   

func Marshal(v interface{}) ([]byte, error) 

 json 转其他类型  

func Unmarshal(data []byte, v interface{}) error

 

 

  • 结构体生成json 
/*  1、结构体转json   json.Marshal*/package mainimport (    "encoding/json"    "fmt")type IT struct {    Company  string   `json:"-"`        // 不解析    Subjects []string `json:"subjects"` //小写    Isok     bool     `json:",string"`  // 转nstring    Price    float64  `json:",string"`  //转nsstring}func main() {    it := IT{
"itcast", []string{
"go", "c++", "test"}, true, 990.232} // buf, error := json.Marshal(it) buf, error := json.MarshalIndent(it, "", "") //格式化 if error != nil { fmt.Println("email====yes", error) return } else { json := string(buf) fmt.Println(json) // /* { "subjects": [ "go", "c++", "test" ], "Isok": "true", "Price": "990.232" } */ }}
  •  map转json
/*  1、map转json   json.Marshal*/package mainimport (    "encoding/json"    "fmt")type IT struct {    Company  string    Subjects []string    Isok     bool    Price    float64}func main() {    // 创建map    m := make(map[string]interface{}, 4)    m["company"] = "google"    m["subjects"] = []string{
"go", "c++", "test"} m["price"] = 888.88 m["isok"] = true buf, error := json.Marshal(m) if error != nil { fmt.Println("email====yes", error) return } else { json := string(buf) fmt.Println(json) //{"Company":"google","Isok":true,"Price":888.88,"Subjects":["go","c++","test"]} }}
  • json转结构体
/*  1、json 转结构体     json.Unmarshal([]byte(jsonBuf), &it)*/package mainimport (    "encoding/json"    "fmt")type IT struct {    Company  string    Subjects []string    Isok     bool    Price    float64}func main() {    jsonBuf := `    {    "company": "itcast",    "subjects": [        "Go",        "C++",        "Python",        "Test"    ],    "isok": true,    "price": 666.666    }`    var it IT    json.Unmarshal([]byte(jsonBuf), &it)    fmt.Println(it) //{itcast [Go C++ Python Test] true 666.666}}
  • 解析到map
  • /*  1、json 转map     json.Unmarshal([]byte(jsonBuf), &it)*/package mainimport (    "encoding/json"    "fmt")func main() {    jsonBuf := `    {    "company": "itcast",    "subjects": [        "Go",        "C++",        "Python",        "Test"    ],    "isok": true,    "price": 666.666    }`    var mapResult map[string]interface{}    json.Unmarshal([]byte(jsonBuf), &mapResult)    fmt.Printf("m====%+v\n", mapResult)    //map[subjects:[Go C++ Python Test] isok:true price:666.666 company:itcast]    for key, value := range mapResult {        //        fmt.Printf("%v ===========%v\n", key, value)        switch data := value.(type) {        case string:            fmt.Printf("map[%s] =====string=====%s\n", key, data)        case bool:            fmt.Printf("map[%s] ======bool====%t\n", key, data)        case float64:            fmt.Printf("map[%s] ======float64====%f\n", key, data)        case []interface{}:            fmt.Printf("map[%s] ======[]interface{}====%s\n", key, data)        }    }}

     

转载于:https://www.cnblogs.com/lpwlpw/p/9999692.html

你可能感兴趣的文章
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>