Go语言转换JSON数据真是非常的简单。
以EasyUI的Demo为例,
将/demo/datagrid/datagrid_data1.json 拷贝到$GOPATH/src目录
JSON.go :
将JSON绑定到结构体,结构体的字段一定要大写,
否则不能绑定数据。
以EasyUI的Demo为例,
将/demo/datagrid/datagrid_data1.json 拷贝到$GOPATH/src目录
JSON.go :
- package main
-
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- )
-
- type product struct {
- Productid string
- Productname string
- Unitcost float32
- Status string
- Listprice float32
- Attr1 string
- Itemid string
- }
-
- type grid struct {
- Total int
- Rows []product
- }
-
- func main() {
- var grid grid
- data, err := ioutil.ReadFile("datagrid_data1.json")
- if err != nil {
- fmt.Println("ReadFile:", err.Error())
- }
- json.Unmarshal(data, &grid)
- fmt.Println(grid)
- fmt.Println("----------------------------")
- b, _ := json.Marshal(grid)
- fmt.Println(string(b))
- }
否则不能绑定数据。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29254281/viewspace-1258391/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29254281/viewspace-1258391/