您现在的位置是:网站首页> 编程资料编程资料
Go语言中转换JSON数据简单例子_Golang_
2023-05-26
364人已围观
简介 Go语言中转换JSON数据简单例子_Golang_
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))
}
将JSON绑定到结构体,结构体的字段一定要大写,否则不能绑定数据。
相关内容
- Go语言操作mysql数据库简单例子_Golang_
- Go语言实现简单的一个静态WEB服务器_Golang_
- 理解Golang中的数组(array)、切片(slice)和map_Golang_
- Golang实现的聊天程序服务端和客户端代码分享_Golang_
- Go语言并发模型的2种编程方案_Golang_
- Go语言中new()和 make()的区别详解_Golang_
- Go语言的方法接受者类型用值类型还是指针类型?_Golang_
- Go语言中的方法、接口和嵌入类型详解_Golang_
- Go语言中的Array、Slice、Map和Set使用详解_Golang_
- Go语言中使用 buffered channel 实现线程安全的 pool_Golang_
