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的更多相关文章

  1. golang基础知识之encoding/json package

    golang基础知识之json 简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.可以去json.org 查看json标准的清晰定义.json pack ...

  2. golang的json操作

    package main import ( "encoding/json" "fmt" "os" ) type ConfigStruct s ...

  3. 48 【golang】json的效率

    本文将主要做如下几方面的测试: 1,构造一个[100]struct的数组,然后来测试它的json编码后的字符串 或者([]byte),首先关心它的功能是否正常: 2,在很早之前,我们在使用golang ...

  4. golang的json序列化问题

    首先看一段代码: package main import ( "encoding/json" "fmt" ) type Result struct { //st ...

  5. go标准库的学习-encoding/json

    参考https://studygolang.com/pkgdoc 导入方式: import "encoding/json" json包实现了json对象的编解码,参见RFC 462 ...

  6. Go_14:GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  7. golang 自定义json解析

    在实际开发中,经常会遇到需要定制json编解码的情况. 比如,按照指定的格式输出json字符串, 又比如,根据条件决定是否在最后的json字符串中显示或者不显示某些字段. 如果希望自己定义对象的编码和 ...

  8. Golang 处理 Json(二):解码

    golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...

  9. Golang 处理 Json(一):编码

    JSON 是一种数据格式描述语言.以 key 和 value 构成的哈系结构,类似 Javascript 中的对象,python 中的字典.通常 json 格式的 key 是字符串,其值可以是任意类型 ...

随机推荐

  1. Codeforces 344A Magnets

    Description Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dom ...

  2. 如何解决winxp访问win10共享打印机提示凭据不足

    Winxp访问win10共享的打印机,提示凭据不足时,如何解决,本篇经验将和大家分享下解决的办法,希望对大家的工作有所帮助! 方法/步骤     在win10的电脑上对着始按钮点鼠标右键,点击运行,或 ...

  3. MVC权限验证过滤器

    Action属性,权限设定属性   [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] ...

  4. nginx下修改svn配置

    最近公司的SVN服务器地址做了变更,而我用的操作系统是Ubuntu操作系统,我也不想把以前下载的代码重新进行修改,我想通过修改svn地址,应该可以,终于在网上通过查找资料,找到了解决的方法:      ...

  5. Swift学习笔记(15)--下标脚本(Subscripts)

    下标脚本可以定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中,使用中类似数组或者字典的用法 1.定义 定义下标脚本使用subscript关键字,语法: s ...

  6. 【Henu ACM Round#14 F】 President and Roads

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出起点到任意点的最短路以及最短路条数=>dis[0][i],cnt[0][i] 然后 把所有的边反向 处理出在反图上终点到 ...

  7. Windows Server 2016 辅助域控制器搭建

    Windows Server 2016 主域控制器搭建完成后,继续进行辅助域控制器搭建.1.更改服务器的IP地址2.修改服务器的名称3.打开服务器管理器,选择添加角色和功能4.选择,下一步5.选择,下 ...

  8. 洛谷—— P1086 花生采摘

    https://www.luogu.org/problem/show?pid=1086#sub 题目描述 鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着 ...

  9. (一一〇)正則表達式的基本使用与RegexKitLite的使用

    正則表達式经常常使用于匹配keyword,以下先介绍基本的语法. [基本的语法] ①中括号表示满足当中之中的一个就可以,比如[abc],则这个位置能够是a.b.c中随意一个. ②在中括号里,能够通过- ...

  10. Tools-->SQL Server Profiler监视数据库

    http://www.cnblogs.com/lorking/p/4062787.html https://docs.microsoft.com/en-us/sql/tools/sql-server- ...