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

  1. [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类

    [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...

  2. ecshop json类的使用

    ecshop中有2个地方使用了json,一个是cls_json.php文件,一个是transport.js文件. cls_json 封装了json类,可以调用里面的encode的方法,根据参数不同,进 ...

  3. php递归json类实例代码

    这篇文章主要介绍了php递归json类的实现方法,可以实现对索引数组及数字数组的解析,进而实现递归数组返回json字符串的功能.具体实现代码如下: <?php /* * @ anthor:QD ...

  4. C#代码 json类

    using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...

  5. C# 转换Json类

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.R ...

  6. Visual Studio自动生成XML类和JSON类

    Visual Studio 2013及以上版本提供了一个非常实用的功能.可以根据xml文档或json文档自动生成类.有了这个工具反序列化时就不用再手动写类并且实现了强类型操作. 步骤 1. 准备一份j ...

  7. Delphi 10.2 Tokyo新增JSON类学习——TJsonSerializer

    Delphi 10.3.2 for windows 7 编译通过,源码下载地址: Tokyo 10.2新增类,效率更高更快 TJsonSerializer 需要引用单元:System.JSON.Ser ...

  8. 【Mysql】Mysql Json类型或Text类型可以建索引吗?

    一.JSON类型 答案是不可以 为Json类型建索引会报错 mysql)); ERROR (): JSON column 'card_pay_data' cannot be used in key s ...

  9. 【JSON类】使用说明

    理解键名路径 键名路径(keyPath)用于定位json的键,比如:{book: {title:”中国人”} },键名路径 book.title 表定位到book下的title键. 对于值是数组类型的 ...

  10. C# 在知道对象时编译json 而不调用json类

    StringBuilder sb = new StringBuilder();            sb.Append('[');                       foreach (va ...

随机推荐

  1. 激光三角测量(sheet of light)halcon示例详解 Reconstruct_Connection_Rod_Calib.hdev 三维重建

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11555100.html 前言:最近项目用到halcon的3d模板匹配,三维重建,相机标定,所以 ...

  2. 【带着canvas去流浪(14)】Three.js中凹浮雕模型的生成方式

    目录 一. 方案1:ThreeBSP.js或ThreeCSG.js扩展库 二. 方案2:平面镂空模型拉伸 三. 方案3:Cinema 4D建模后输出模型文件 示例代码托管在:http://www.gi ...

  3. SpringBoot + Jpa(Hibernate) 架构基本配置

    1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...

  4. java.sql.SQLException: Data truncation: Incorrect string value: '\xE5\x91\xA8\xE6\x9D\xBE' for column 'cname' at row 1 Query

    在将项目上传到服务器时,发生这样的错误,总结了一下. 环境Centos7+tomcat7+Mariadb数据库 首先mysql -uroot -p 输入密码 然后查询数据库当前码表状态 show va ...

  5. python语言程序设计基础 习题 天天向上的力量(math.pow)

    实例1: 一年365天,,以第一天的能力值为基数1.0,当好好学习时能力值相比前一天提高1%,没有学习时能力值相比前天要降低1%,每天努力和每天放任,一年下来的能力值相差多少? 解析: 如果每天都好好 ...

  6. Git初始化项目 和 Gitignore

    初始化init: git init git status git add . git commit -am "init projrct" 添加远程仓库: git remote ad ...

  7. hadoop集群单点配置

    =================== =============================== ----------------hadoop集群搭建 --------------------- ...

  8. Maven 梳理-自动创建Maven项目(非web)

    mvn archetype:create和mvn archetype:generate create is deprecated in maven 3.0.5 and beyond,在maven3.0 ...

  9. Git很麻烦?不存在的!掌握这几招就够了

    废话不多说,下面直接开始了! 查看原文 确保代码库是最新的,先用这条命令把你的代码拉取到本地 git clone -- 修改完代码后,按顺序执行下面四个命令 git pull git add * /r ...

  10. iOS开发进阶(唐巧)读书笔记(一)

    如何提高iOS开发技能 1.阅读博客:https://github.com/tangqiaoboy/iOSBlogCN 40多位iOS开发博主的博客地址 2.读书:每年阅读一本高质量的iOS开发书籍 ...