当前位置:网站首页>Golang对JSON文件的读写操作
Golang对JSON文件的读写操作
2022-06-23 15:38:00 【学亮编程手记】
Go语言JSON文件的读写操作
写
package main
import (
"encoding/json"
"fmt"
"os"
)
type Website struct {
Name string `xml:"name,attr"`
Url string
Course []string
}
func main() {
info := []Website{
{
"Golang", "http://c.biancheng.net/golang/", []string{
"http://c.biancheng.net/cplus/", "http://c.biancheng.net/linux_tutorial/"}}, {
"Java", "http://c.biancheng.net/java/", []string{
"http://c.biancheng.net/socket/", "http://c.biancheng.net/python/"}}}
// 创建文件
filePtr, err := os.Create("info.json")
if err != nil {
fmt.Println("文件创建失败", err.Error())
return
}
defer filePtr.Close()
// 创建 json 编码器
encoder := json.NewEncoder(filePtr)
err = encoder.Encode(info)
if err != nil {
fmt.Println("编码错误", err.Error())
} else {
fmt.Println("编码成功")
}
}
读
package main
import (
"encoding/json"
"fmt"
"os"
)
type Website struct {
Name string `xml:"name,attr"`
Url string
Course []string
}
func main() {
filePtr, err := os.Open("./info.json")
if err != nil {
fmt.Println("文件打开失败 [Err:%s]", err.Error())
return
}
defer filePtr.Close()
var info []Website
// 创建json解码器
decoder := json.NewDecoder(filePtr)
err = decoder.Decode(&info)
if err != nil {
fmt.Println("解码失败", err.Error())
} else {
fmt.Println("解码成功")
fmt.Println(info)
}
}
/** 解码成功 [{Golang http://c.biancheng.net/golang/ [http://c.biancheng.net/cplus/ http://c.biancheng.net/linux_tutorial/]} {Java http://c.biancheng.net/java/ [http://c.biancheng.net/socket/ http://c.biancheng.net/python/]}] `` */
边栏推荐
- uniapp对接腾讯即时通讯TIM 发图片消息问题
- Web容器是怎样给第三方插件做初始化工作的
- 【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
- Summarize the experience of purchasing Alibaba cloud servers
- If no code is moved, the project access speed drops significantly the next day. Case analysis
- Understand the classic buck-boost negative voltage circuit
- 坚持五件事,带你走出迷茫困境
- 图片读取:Image.open(ImgPath)
- 规避这六大难点,成功实施MES系统
- [tcapulusdb knowledge base] Introduction to tmonitor background one click installation (II)
猜你喜欢
随机推荐
js 递归json树 根据 子id 查 父id
ABP framework - data access infrastructure (Part 2)
R语言使用timeROC包计算无竞争情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、可视化无竞争情况下的生存资料多时间ROC曲线
《ThreadLocal》
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
Analysis of TCP three-time handshake and four-time handshake
If no code is moved, the project access speed drops significantly the next day. Case analysis
CAS操作在ARM和x86下的不同实现
总结一下购买阿里云服务器的经验
PageHelper faces the paging problem of complex service data processing
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
js 对象 使用小技巧
图片保存:torchvision.utils.save_image(img, imgPath)
Array's own method
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
Introduction to asynccontext
15 differences between MES in process and discrete manufacturing enterprises (Part I)
Use of iscellstr function in MATLAB
The meaning of FPGA abbreviations and words in engineering field
ADB key name, key code number and key description comparison table








