json文件解析

注意:只有在结构体中以大写字母开头的字段才能被解析!!!

参考链接:https://www.jianshu.com/p/7aa743717c93

package main

import (
    "encoding/json"
    "fmt"
)

type Server struct {
    ServerName string
    ServerIP   string
}

type Serverslice struct {
    Servers []Server
}

func main() {
    var s Serverslice
    str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"},{"serverName":"Guangzhou_VPN","serverIP":"127.0.0.3"}]}`
    json.Unmarshal([]byte(str), &s)
    fmt.Println(s)
}

上述代码的输出为:

{[{Shanghai_VPN 127.0.0.1} {Beijing_VPN 127.0.0.2} {Guangzhou_VPN 127.0.0.3}]}

  

  

posted @ 2019-12-17 22:30  `'手可摘星辰  阅读(335)  评论(0编辑  收藏  举报