前端之家收集整理的这篇文章主要介绍了
golang json处理,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main
import (
"encoding/json"
"fmt"
"os"
)
type ConfigStruct struct {
Host string `json:"host"`
Port int `json:"port"`
AnalyticsFile string `json:"analytics_file"`
StaticFileVersion int `json:"static_file_version"`
StaticDir string `json:"static_dir"`
TemplatesDir string `json:"templates_dir"`
SerTcpSocketHost string `json:"serTcpSocketHost"`
SerTcpSocketPort int `json:"serTcpSocketPort"`
Fruits []string `json:"fruits"`
}
type Other struct {
SerTcpSocketHost string `json:"serTcpSocketHost"`
SerTcpSocketPort int `json:"serTcpSocketPort"`
Fruits []string `json:"fruits"`
}
func main() {
jsonStr := `{"host": "http://localhost:9090","port": 9090,"analytics_file": "","static_file_version": 1,"static_dir": "E:/Project/goTest/src/","templates_dir": "E:/Project/goTest/src/templates/","serTcpSocketHost": ":12340","serTcpSocketPort": 12340,"fruits": ["apple","peach"]}`
//json str 转map
var dat map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr),&dat); err == nil {
fmt.Println("==============json str 转map=======================")
fmt.Println(dat)
fmt.Println(dat["host"])
}
//json str 转struct
var config ConfigStruct
if err := json.Unmarshal([]byte(jsonStr),&config); err == nil {
fmt.Println("================json str 转struct==")
fmt.Println(config)
fmt.Println(config.Host)
}
//json str 转struct(部份字段)
var part Other
if err := json.Unmarshal([]byte(jsonStr),&part); err == nil {
fmt.Println("================json str 转struct==")
fmt.Println(part)
fmt.Println(part.SerTcpSocketPort)
}
//struct 到json str
if b,err := json.Marshal(config); err == nil {
fmt.Println("================struct 到json str==")
fmt.Println(string(b))
}
//map 到json str
fmt.Println("================map 到json str=====================")
enc := json.NewEncoder(os.Stdout)
enc.Encode(dat)
//array 到 json str
arr := []string{"hello","apple","python","golang","base","peach","pear"}
lang,err := json.Marshal(arr)
if err == nil {
fmt.Println("================array 到 json str==")
fmt.Println(string(lang))
}
//json 到 []string
var wo []string
if err := json.Unmarshal(lang,&wo); err == nil {
fmt.Println("================json 到 []string==")
fmt.Println(wo)
}
}
b :=new(bytes.Buffer)
encodeErr := json.NewEncoder(b).Encode(payload)
if encodeErr != nil {return encodeErr}
package main
// xiaorui.cc
import "fmt"
import "encoding/json"
type Detail struct {
Time string `json:"time"`
Ops string `json:"ops"`
}
type Version struct {
Last string `json:"last"`
Max int `json:"max"`
Detail []Detail `json:"detail"`
}
type Desc struct {
Date string `json:"date"`
Content string `json:"content"`
Brief string `json:"brief"`
Keyword string `json:"keyword"`
Version Version `json:"version"`
}
type Response struct {
Url string `json:"url"`
Title string `json:"title"`
Email string `json:"email"`
Admin string `json:"admin"`
Address []string `json:"address"`
Article []Desc `json:"article"`
}
func main() {
body := `
{
"url": "http://xiaorui.cc","title": "golang and python","admin": "fengyun","email": "rfyiamcool@163.com","address": [
"beijing","qingdao"
],"article": [
{
"date": "2014","content": "golang json push to redis server","brief": "golang json","keyword": "json","version": {
"max": 3,"last": "2016-03-11","detail": [
{
"time": "2016-03-12","ops": "add my email"
}
]
}
}
]
}
`
var r Response
err := json.Unmarshal([]byte(body),&r)
if err != nil {
fmt.Printf("err was %v",err)
}
fmt.Println(r.Article[0].Version.Max)
fmt.Println(r.Article[0].Version.Detail[0].Ops)
}
原文链接:https://www.f2er.com/go/188758.html