golang之Json编码解码

栏目: Go · 发布时间: 5年前

内容简介:编码json.Marshal()func Marshal(v interface{}) ([]byte, error)

编码

json.Marshal()

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

解码

json.Unmarshal()

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

json TO struct在线转译 : http://json2struct.mervine.net/

json to go滴滴团队研发(据说比官方快6倍): https://github.com/json-iterator/go

Warning:Json转结构体,或结构体转Json,结构体首字母必须大写!!!

​
package main

import (
    "fmt"
    "encoding/json"
)

func main() {
    type Student struct {
        Name string
        Age int
        Classes []string
        Prices float64
    }

    st := &Student {
        "xiaoming",
        18,
        []string{"math","english","chinese"},
        9.99,
    }

    result, err := json.Marshal(st)
    if(err == nil) {
        fmt.Println(string(result))
    }
    
}   

//{"Name":"xiaoming","Age":18,"Classes":["math","english","chinese"],"Prices":9.99}

​

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learn Python 3 the Hard Way

Learn Python 3 the Hard Way

Zed A. Shaw / Addison / 2017-7-7 / USD 30.74

You Will Learn Python 3! Zed Shaw has perfected the world’s best system for learning Python 3. Follow it and you will succeed—just like the millions of beginners Zed has taught to date! You bring t......一起来看看 《Learn Python 3 the Hard Way》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具