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)
    
            }
    
        }
    }
原文地址:https://www.cnblogs.com/lpwlpw/p/9999692.html