Golang - 处理json

1. 编码json

  • 使用json.Marshal()函数可以对一组数据进行JSON格式的编码

  • func Marshal(v interface{}) ([]byte, error)

  • 通过结构体生成json,结构体属性字段名首字母要大写

      package main
    
      import (
    "encoding/json"
    "fmt"
    ) type Person struct {
    Name string
    Hobby string
    } func main() {
    p := Person{"luhan", "男"}
    //1.生成JSON文本
    b, err := json.Marshal(p)
    if err != nil {
    fmt.Println("json err:", err)
    }
    fmt.Println(string(b))
    //2.生成格式化json,没啥用
    b, err = json.MarshalIndent(p, "", " ")
    if err != nil {
    fmt.Println("json err:", err)
    }
    fmt.Println(string(b))
    }

struct tag

-是隐藏字段

也可以直接写别名,包含首字母小写的单词

通过map生成json

package main

import (
"encoding/json"
"fmt"
) func main() {
//创建map
mmp := make(map[string]interface{})
mmp["name"] = "luhan"
mmp["age"] = 56
mmp["niubility"] = true //map转为json
mjson, err := json.Marshal(mmp)
if err != nil {
fmt.Println("json err:", err)
}
fmt.Println(string(mjson))
}

2. 解码json

解析到结构体

package main

import (
"encoding/json"
"fmt"
) type Person struct {
Name string `json:"name"`
Hobby string `json:"hobby"`
} func main() {
//1.准备一段json
b := []byte(`{"Name":"luhan","Hobby":"男"}`)
//2.声明结构体
var p Person
//3.解析
err := json.Unmarshal(b, &p)
if err != nil {
fmt.Println("json err:", err)
}
fmt.Println(p)
}

解析到interface

package main

import (
"encoding/json"
"fmt"
) func main() {
//1.准备一段json
b := []byte(`{"Name":"luhan","Hobby":"男"}`)
//2.声明空接口
var i interface{}
//3.解析
err := json.Unmarshal(b, &i)
if err != nil {
fmt.Println("json err:", err)
}
//自动转map
fmt.Println(i)
//4.使用interface的json,可以判断类型
m := i.(map[string]interface{})
for k, v := range m {
switch vv := v.(type) {
case string:
fmt.Println(k, "是string类型", vv)
case int:
fmt.Println(k, "是int类型", vv)
default:
fmt.Println(k, "是其他类型")
}
}
}

Golang - 处理json的更多相关文章

  1. 48 【golang】json的效率

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

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

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

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

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

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

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

  5. GoLang中 json、map、struct 之间的相互转化

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

  6. golang解析json报错:invalid character '\x00' after top-level value

    golang解析json报错:invalid character '\x00' after top-level value 手动复制字符串:{"files":["c:/t ...

  7. golang webservice[ json Martini webframe]

    golang webservice[ json Martini webframe] https://github.com/brunoga/go-webservice-sample 自己修改了一下例子, ...

  8. Golang的json包

    encoding/json encoding/json是官方提供的标准json, 实现RFC 7159中定义的JSON编码和解码.使用的时候需要预定义struct,原理是通过reflection和in ...

  9. Golang解析json的几种方法

    Golang解析json的几种方法 概要 使用Golang调用其它平台API接口时总会被多层的json串给恶心到,我记录一下自己解析json的几种方法. 一.自带的json包 func JsonUnm ...

随机推荐

  1. ListView的基本使用技巧

    ListView的基本使用技巧 1.headerView和footerView 2.ViewHolder 3.OnScrollListener 4.单行刷新 5.其它细节 ListView提供head ...

  2. HDU1698 Just a Hook 【线段树】+【成段更新】+【lazy标记】

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. LeetCode60:Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. Swift版本UIWebView长按保存图片

    起因 最近需要做个IOS的壳子,用到长按保存图片的功能,发现百度出来的全是OC语法的例子,很多都不是全面,只能自己写一份Swift版本的,图片下面附上Github地址 效果图 Github地址:htt ...

  5. hibernate初步3

    事务和并发 1.事务概念 一组不可分割的操作,事务有如下属性(ACID 属性:Atomic Consistent Isolated Durable)(1)原子性---Atomic  事务的原子性指的是 ...

  6. git拉取远端改变,但是不覆盖本地的修改

    1.git stash 2.git  fetch weixin-old-remote 3.git rebase weixin-old-remote/main151028_wxpay_main15100 ...

  7. POJ 2286 The Rotation Game IDA*

    (再一次感谢学长幻灯片) ID A* 随便自己yy了一下. 额嗯 思路什么的都没有问题 就是改不对.. 无奈地删代码...边删边交. 删啊删 哎呦 AC了 ... ... ... 找删的那一段 . o ...

  8. indeed 4.22 第一次网测

    1.第一题 没有看 2. 由于数据范围很小,所以每一层需要全排列,寻找最小的花费,然后所有层加起来就是最后的结果. #include<bits/stdc++.h> #define pb p ...

  9. X - Vasya and Socks

    Problem description Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pai ...

  10. ACM_开心消消乐

    开心消消乐 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大白最近喜欢上了开心消消乐,于是英语基础好的他准备让课文中英语句子也来 ...