zoukankan      html  css  js  c++  java
  • go_json解析

     总结:

     其他类型转json   

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

     json 转其他类型  

    func Unmarshal(data []byte, v interface{}) error
    • 结构体生成json 
    /*
      1、结构体转json   json.Marshal
    */
    
    package main
    
    import (
        "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 main
    
    import (
        "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 main
    
    import (
        "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 main
      
      import (
          "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
      ", mapResult)
          //map[subjects:[Go C++ Python Test] isok:true price:666.666 company:itcast]
      
          for key, value := range mapResult {
      
              //        fmt.Printf("%v ===========%v
      ", key, value)
      
              switch data := value.(type) {
              case string:
                  fmt.Printf("map[%s] =====string=====%s
      ", key, data)
      
              case bool:
                  fmt.Printf("map[%s] ======bool====%t
      ", key, data)
      
              case float64:
                  fmt.Printf("map[%s] ======float64====%f
      ", key, data)
      
              case []interface{}:
      
                  fmt.Printf("map[%s] ======[]interface{}====%s
      ", key, data)
      
              }
      
          }
      }
  • 相关阅读:
    自然数e为底数的指数函数的一个小运用
    Windows产品测试集合整理
    随手写的 IniFiles
    Windows C++ TLS 实现连接163邮箱
    Windows创建个人证书(C++实现,使用 as administrator)
    单进程单线程IOCP的实现(含客户端和服务端)
    32/64位下面的基本数据类型的大小
    WMI 获取操作系统名称和版本
    http 基本代理 C++实现(极简)
    获取内存大小、CPU大小、硬盘大小及使用率
  • 原文地址:https://www.cnblogs.com/lpwlpw/p/9999692.html
Copyright © 2011-2022 走看看