Go语言中转换JSON数据简单例子
作者:junjie 时间:2024-04-26 17:32:28
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绑定到结构体,结构体的字段一定要大写,否则不能绑定数据。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
隐蔽的ASP后门 大家可以查看下
细化解析:怎样把你的MySQL完全中文化
Python字典实现简单的三级菜单(实例讲解)
全面剖析Python的Django框架中的项目部署技巧第1/2页
Python实现的爬取豆瓣电影信息功能案例
![](https://img.aspxhome.com/file/2023/2/60742_0s.png)
Golang中interface的基本用法详解
![](https://img.aspxhome.com/file/2023/7/99637_0s.png)
php对数字进行万、亿单位的转化
python通过pil将图片转换成黑白效果的方法
深入学习JavaScript 高阶函数
python 获取计算机的网卡信息
微信支付PHP SDK之微信公众号支付代码详解
![](https://img.aspxhome.com/file/2023/0/126380_0s.png)
Python多线程结合队列下载百度音乐的方法
oblog4.6转换ucenterHome1.5过程全记录,提供老用户无法登陆的补丁
七种Python代码审查工具推荐
Python生成rsa密钥对操作示例
使用python获取电脑的磁盘信息方法
JS+CSS模拟可以无刷新显示内容的留言板实例
![](https://img.aspxhome.com/file/2023/3/135863_0s.jpg)
python pytorch图像识别基础介绍
![](https://img.aspxhome.com/file/2023/4/134584_0s.png)
PowerBI和Python关于数据分析的对比
![](https://img.aspxhome.com/file/2023/8/117988_0s.gif)