1 Star 0 Fork 0

dl615/gotest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
json.go 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
dilei 提交于 2019-12-31 19:48 . update
package main
import (
"encoding/json"
"fmt"
"strconv"
)
type Server struct {
ServerName string
ServerIP string
Id int `json:"id,string"`
Num int `json:"num"`
}
type Serverslice struct {
Servers []Server
}
func main() {
var s Serverslice
// ok
str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1","id":"1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2","id":"2"}]}`
// json: invalid use of ,string struct tag, trying to unmarshal unquoted value into int
// str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1","id":1},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2","id":2}]}`
// json: cannot unmarshal string into Go struct field Server.Servers.num of type int
// str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1","id":"1","num":"123"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2","id":"2","num":"1234"}]}`
// json: cannot unmarshal number into Go struct field Server.Servers.ServerIP of type string
// str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":12312,"id":"1","num":"123"},{"serverName":"Beijing_VPN","serverIP":1231,"id":"2","num":"1234"}]}`
fmt.Println(s)
var ss interface{}
str = `{}`
err := json.Unmarshal([]byte(str), &s)
if err != nil {
fmt.Printf(err.Error())
}
fmt.Println(s)
switch ss.(type) {
case Serverslice:
fmt.Println("serverslice")
default:
fmt.Println("not serverslice")
}
byteArr, _ := json.Marshal(s)
fmt.Println(string(byteArr))
fmt.Println(fmt.Sprintf("%.02f", 1.2))
fmt.Println(fmt.Sprintf("%.2f", 1.2))
val, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", 1.3), 64)
fmt.Println(val)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dl615/gotest.git
git@gitee.com:dl615/gotest.git
dl615
gotest
gotest
master

搜索帮助