章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • python

  • shell

  • go

    • go基础

      • 指针
      • 数组
      • 切片
      • 字典
      • 结构体
      • 匿名组合
      • 方法
      • 接口
      • error接口
      • panic使用
    • Init函数和main函数
    • 下划线
    • go报错问题收集
    • Redis和MySQL结合的Web服务示例
    • go定义json数据
    • 使用go和vue编写学生管理系统
    • gin框架探索
  • 编程
  • go
章工运维
2024-09-01

go定义json数据

代码如下

package main

import (
	"encoding/json"
	"fmt"
)

type User struct {
	Name  string `json:"name"`
	Email string `json:"email"`
	Age   int    `json:"age"`
}

func main() {
	//编码
	user := User{Name: "zhangsan", Email: "123@qq.com", Age: 12}
	jsonData, err := json.Marshal(user)
	if err != nil {
		fmt.Println("编码错误", err)
		return
	}
	fmt.Println("编码后的json", string(jsonData))
	//解码
	jsonString := `{"name":"lisi", "email":"fd@11.cn", "age":12}`
	var decodeUser User
	err = json.Unmarshal([]byte(jsonString), &decodeUser)
	if err != nil {
		println("解码错误", err)
		return
	}
	fmt.Printf("解码后的json %+v\n", decodeUser)

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
微信 支付宝
上次更新: 2024/09/01, 20:26:32

← Redis和MySQL结合的Web服务示例 使用go和vue编写学生管理系统→

最近更新
01
shell脚本模块集合
05-13
02
生活小技巧(认知版)
04-29
03
生活小技巧(防骗版)
04-29
更多文章>
Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式