go-json类
package main import (
"encoding/json"
"fmt"
) /*
{
"company":"itcast"
"":[
"go"
"c++"
],
"isok":"ok"
"price":"666"
}
*/
//成员变量名首字母必须大写
type IT struct {
Company string `json:"company"` //变为小写
Subject []string
Isok bool `json:",string"`
Price float32
} type IT2 struct {
Company string `json:"company"`
Subject []string `json:"subject"`
Isok bool `json:"isok"`
Price float32 `json:"price"`
} func main() {
s := IT{"itcast", []string{"go", "c++"}, true, } buf, err := json.Marshal(s)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))
//{"Company":"itcast","Subject":["go","c++"],"Isok":true,"Price":666} //格式化编码
buf2, err := json.MarshalIndent(s, "", " ")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(buf2)) //通过Map生成json
m := make(map[string]interface{}, )
m["compary"] = "itcast"
m["subjects"] = []string{"go", "c++"}
m["Isok"] = true
m["price"] = result, err2 := json.Marshal(m)
if err2 != nil {
return
}
fmt.Println(string(result)) //json 解析到 结构体
jsonbuf := `{"Isok":true,"compary":"itcast","price":,"subjects":["go","c++"]}` var tmp IT2
err3 := json.Unmarshal([]byte(jsonbuf), &tmp)
if err3 != nil {
fmt.Println(err3)
return
}
fmt.Println(tmp) //json 解析到map
m2 := make(map[string]interface{}, )
err4 := json.Unmarshal([]byte(jsonbuf), &m2)
if err4 != nil {
fmt.Println(err4)
}
fmt.Println("map = ", m2) //取元素
for key, value := range m {
fmt.Println(key, value)
switch data := value.(type) {
case string:
fmt.Println(data)
case int:
fmt.Println(data)
}
}
}
go-json类的更多相关文章
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- ecshop json类的使用
ecshop中有2个地方使用了json,一个是cls_json.php文件,一个是transport.js文件. cls_json 封装了json类,可以调用里面的encode的方法,根据参数不同,进 ...
- php递归json类实例代码
这篇文章主要介绍了php递归json类的实现方法,可以实现对索引数组及数字数组的解析,进而实现递归数组返回json字符串的功能.具体实现代码如下: <?php /* * @ anthor:QD ...
- C#代码 json类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- C# 转换Json类
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.R ...
- Visual Studio自动生成XML类和JSON类
Visual Studio 2013及以上版本提供了一个非常实用的功能.可以根据xml文档或json文档自动生成类.有了这个工具反序列化时就不用再手动写类并且实现了强类型操作. 步骤 1. 准备一份j ...
- Delphi 10.2 Tokyo新增JSON类学习——TJsonSerializer
Delphi 10.3.2 for windows 7 编译通过,源码下载地址: Tokyo 10.2新增类,效率更高更快 TJsonSerializer 需要引用单元:System.JSON.Ser ...
- 【Mysql】Mysql Json类型或Text类型可以建索引吗?
一.JSON类型 答案是不可以 为Json类型建索引会报错 mysql)); ERROR (): JSON column 'card_pay_data' cannot be used in key s ...
- 【JSON类】使用说明
理解键名路径 键名路径(keyPath)用于定位json的键,比如:{book: {title:”中国人”} },键名路径 book.title 表定位到book下的title键. 对于值是数组类型的 ...
- C# 在知道对象时编译json 而不调用json类
StringBuilder sb = new StringBuilder(); sb.Append('['); foreach (va ...
随机推荐
- Java8之熟透Optional
一.使用Optional引言 1.1.代码问题引出 在写程序的时候一般都遇到过 NullPointerException,所以经常会对程序进行非空的判断: User user = getUserByI ...
- Codeforces Round #585 (Div. 2)
https://www.cnblogs.com/31415926535x/p/11553164.html 感觉很硬核啊这场,,越往后越做不动,,,emmmm,,,(这场是奔着最后一题 2sat 来的, ...
- TestNG(十) 依赖测试
package com.course.testng.suite; import org.testng.annotations.Test; public class DepenTest { @Test ...
- log4j日志不输出的问题
今天服务器上报错,想先去看一下日志进行排查,结果发现日志很久都没有输出过了.从上午排查到下午,刚刚解决,因此记录一下,但现在也只是知其然,并不知其所以然,所以如果大家有什么想法请在下方评论. 先说一下 ...
- 集群某节点DataNode服务无法启动解决(报java.net.BindException:Address already in use错误)
现象: 在集群中某节点, 启动DataNode服务后马上又Shutdown, 在操作系统没看到有DataNode的日志(可能是服务启动失败, 自动删除了日志文件),幸好在界面上可以查看报错的日志: ...
- vscode中如何自动保存
是的,vscode是个不错的编辑器,它的扩展功能能支持很多的语言,然后在实践过程中,我们发现每写好一次就得手动按CTRL+S,未免有点手酸,这时候我们就可以开启我们的自动保存功能,方式也很简单,在 文 ...
- Scala Data Structure
Arrays Array 固定长度:ArrayBuffer 可变长度 arr.toBuffer, buf.toArray 初始化是不要使用 new 使用 () 访问元素 使用 for (elem &l ...
- springboot 使用freemarker自定义标签
1.pom依赖引入 <dependencies> <dependency> <groupId>org.springframework.boot</groupI ...
- Spring Cloud 版本控制
### 正常版本 ``` org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE ``` ### SpringCloud 版 ...
- JAVA错误提示:The operation is not applicable to the current selection.Select a field which is not declared as type variable or a type that declares such fields.
平时没怎么注意,今天用Eclipse自动生成Set Get方法时提示错误,错误信息如下: The operation is not applicable to the current selectio ...