golang encoding/json
package main import (
"bytes"
"encoding/json"
"fmt"
) type ColorGroup struct {
ID int
Name string
Colors []string
} func main() { //---------------Marshal
group := ColorGroup{
ID: ,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
//os.Stdout.Write(b)
fmt.Println(string(b[:])) //---------------Unmarshal
var jsonBlob = []byte(`[
{"ID":,"Name":"Reds1","Colors":["Crimson","Red1","Ruby1","Maroon1"]},
{"ID":,"Name":"Reds2","Colors":["Crimson","Red2","Ruby2","Maroon2"]},
{"ID":,"Name":"Reds3","Colors":["Crimson","Red3","Ruby3","Maroon3"]}
]`) var animals []ColorGroup
error := json.Unmarshal(jsonBlob, &animals)
if error != nil {
fmt.Println("error:", error)
} //fmt.Printf("%+v", animals)
//fmt.Println(animals)
for i, x := range animals {
fmt.Println(i, x) }
//---------------Indent
dst := new(bytes.Buffer)
json.Indent(dst, jsonBlob, "##", "**")
fmt.Println(dst)
}
golang encoding/json的更多相关文章
- golang基础知识之encoding/json package
golang基础知识之json 简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.可以去json.org 查看json标准的清晰定义.json pack ...
- golang的json操作
package main import ( "encoding/json" "fmt" "os" ) type ConfigStruct s ...
- 48 【golang】json的效率
本文将主要做如下几方面的测试: 1,构造一个[100]struct的数组,然后来测试它的json编码后的字符串 或者([]byte),首先关心它的功能是否正常: 2,在很早之前,我们在使用golang ...
- golang的json序列化问题
首先看一段代码: package main import ( "encoding/json" "fmt" ) type Result struct { //st ...
- go标准库的学习-encoding/json
参考https://studygolang.com/pkgdoc 导入方式: import "encoding/json" json包实现了json对象的编解码,参见RFC 462 ...
- Go_14:GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- golang 自定义json解析
在实际开发中,经常会遇到需要定制json编解码的情况. 比如,按照指定的格式输出json字符串, 又比如,根据条件决定是否在最后的json字符串中显示或者不显示某些字段. 如果希望自己定义对象的编码和 ...
- Golang 处理 Json(二):解码
golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...
- Golang 处理 Json(一):编码
JSON 是一种数据格式描述语言.以 key 和 value 构成的哈系结构,类似 Javascript 中的对象,python 中的字典.通常 json 格式的 key 是字符串,其值可以是任意类型 ...
随机推荐
- 互联网金融研究组:P2P借贷平台:性质、风险与监管(上)
互联网金融研究组(): P2P借贷平台:性质.风险与监管(上) 目 录 一.性质与合法性 1. P2P网络借贷 1.1 概念重新界定 1.2 发展概况与特点 2. 延伸模式及其合法性浅析 2. ...
- Lambda表达式相当于一个函数
看来你对Lambda完全不懂.Lambda表达式相当于一个函数. 比如model => model.Name相当于string 一个函数(Model的类型 model) { return ...
- java中replaceAll反斜杠\ or java中replaceAll 括号[
java中replaceAll反斜杠\ String s=new String("this is a \\"); s.replaceAll("\\",&qu ...
- Network Stack
Network Stack 目录 1 Overview 2 Code Layout 3 Anatomy of a Network Request (focused on HTTP) 3.1 URLRe ...
- Firewalld 用法解析
其实还是我写的啦 https://www.jianshu.com/p/3444d9413461 1.防火墙firewall的基本概述 现在的RedHat/CentOS7版本默认都使用firewall防 ...
- 两种方法解决 "The License CNEKJPQZEX- has been cancelled..." 问题
今天在使用 2017 的 IDEA 和 Pycharm 等IDE的时候,提示了如题的问题.之前实在 http://idea.lanyus.com/ 网站点击生成注册码,复制粘贴到 IDEA 中就好了, ...
- jquery点击弹框外层关闭弹框
$(document).bind("click",function(e){ if($( e.target ).closest(".game-cont ...
- iOS基本UI控件总结
包括以下几类: //继承自NSObject:(暂列为控件) UIColor *_color; //颜色 UIImage *_image; //图像 //继承自UIView:只能相应手势UI ...
- 树莓派开机运行Python脚本 控制LED灯闪烁
一.新建一个开机运行文件 在 /home/pi/.config 下创建一个文件夹,名称为 autostart,并在该文件夹下创建一个led.desktop文件(文件名以.desktop结尾) 编辑le ...
- android反编译odex文件
关于android的反编译工具,相信大家并不陌生 如APK-TOOL,dex2jar APK-TOOL 用于反编译出布局文件 下载地址http://code.google.com/p/android- ...