PHP中高级工程师面试重点讲解视频课程
Go快速入门浅显易懂视频教程-基础篇
Go快速入门浅显易懂视频教程-中级篇
go的json操作
阅读:1710 分享次数:0

package main

import (
    "encoding/json"  
    "fmt"  
)

type Server struct {  
    ServerName string  
    ServerIp   string  
}

type Serverslice struct {  
    Servers []Server  
}

func main() {  
    //解析一个json  
    var s Serverslice  
    str := `{"servers":[{"serverName":"GZZ_VPN","serverIP":"127.0.0.3"},{"serverName":"SZ_VPN","serverIP":"127.0.0.4"}]}`  
    //这里是json_decode  
    json.Unmarshal([]byte(str), &s)  
    fmt.Println(s)  

    //添加一个json
    s.Servers = append(s.Servers, Server{ServerName: "Shanghai_VPN", ServerIp: "127.0.0.1"})
    s.Servers = append(s.Servers, Server{ServerName: "Beijing_VPN", ServerIp: "127.0.0.2"})

    //这里相当于json_encode
    b, err := json.Marshal(s)
    if err != nil {
        fmt.Println("json err:", err)
    }

    fmt.Println(string(b))
}

感觉本站内容不错,读后有收获?