在Go语言程序中使用gojson来解析JSON格式文件

作者:goldensun 时间:2023-07-02 14:19:52 

gojson是快速解析json数据的一个golang包,你使用它可以快速的查找json内的数据
安装


go get github.com/widuu/gojson

使用简介

结构


type Js struct {
    data interface{}
}


(1) func Json(data) *Js data为string类型,初始化Js结构,解析json并且return Js.data


json := `{"from":"en","to":"zh"}`
c1 := gojson.Json(json) //&{map[from:en to:zh]}


(2) func (*Js) Get() *js 获取简单json中的某个值,递归查找,return Js.data


json := `{"from":"en","to":"zh","trans_result":{"src":"today","dst":"\u4eca\u5929"},"result":["src","today","dst","\u4eca\u5929"]}`

c2 := gojson.Json(json).Get("trans_result").Get("dst")
fmt.Println(c2) //&{今天}

c2 := gojson.Json(json).Get("from")
fmt.Println(c2) //&{en}

(3) func (*Js)Tostring()string 将单个数据转化成string类型,因为string类型转其它类型都比较好转就让数据返回string


c2 := gojson.Json(json).Get("from").Tostring()
fmt.Println(c2) //en


(4) func (j *Js) Getpath(args ...string) *Js 通过输入string的多个参数来获取某个值,json数据一定要是递归的


c4 := gojson.Json(json).Getpath("trans_result", "src").Tostring()
fmt.Println(c4)  //today


(5) func (j *Js) Arrayindex(i int) string 获取Json数据中数组结构的值,根据输入的num来返回对应的值,仅限于处理{“result”:[“src”,”today”,”dst”,”\u4eca\u5929″]}中[]内的值


json := `{"from":"en","to":"zh","trans_result":{"src":"today","dst":"\u4eca\u5929"},"result":["src","today","dst","\u4eca\u5929"]}`
c7 := gojson.Json(json).Get("result").Arrayindex(1)
fmt.Println(c7) //src


(6) func (j *Js) Getkey(key string, i int) *Js 这个函数是针对数据中有重复数据,取值,使用js.data必须是[]interface{}类型,这个是百度翻译时候返回的js可能会用到


json1 := `{"from":"en","to":"zh","trans_result":[{"src":"today","dst":"\u4eca\u5929"},{"src":"tomorrow","dst":"\u660e\u5929"}]}`
c8 := gojson.Json(json1).Get("trans_result").Getkey("src", 1).Tostring()
fmt.Println(c8) //则返回trans_result第一组中的src today


(7) func (j *Js) ToArray() (k, d []string)将json数据转换成key []string{} value []string{} 一一对应的数组,只能使用到二级 不能到多级


c9k, c9v := gojson.Json(json1).Get("trans_result").ToArray()
fmt.Println(c9k, c9v) //[src dst src dst] [today 今天 tomorrow 明天]

c3k, c3v := gojson.Json(json).Getindex(1).ToArray()
fmt.Println(c3k, c3v) //    [from] [en]

(8) func (j *Js) Getindex(i int) *Js 根据i返回json内的数据,可以逐级查找


json1 := `{"from":"en","to":"zh","trans_result":[{"src":"today","dst":"\u4eca\u5929"},{"src":"tomorrow","dst":"\u660e\u5929"}]}`

c10 := gojson.Json(json1).Getindex(3).Getindex(1).Getindex(1).Get("src").Tostring()
fmt.Println(c10) //today

(9) func (j *Js) StringtoArray() []string 将{“result”:[“src”,”today”,”dst”,”\u4eca\u5929″]}数据json中的result对应的数据,返回成[]string的slice


c11 := gojson.Json(json).Get("result").StringtoArray()
fmt.Println(c11) //[src today dst 今天]


(10) func (j *Js) Type() 打印测试用,打印数据类型


gojson.Json(json).Get("result").Type()  //[]interface {}

标签:Go,JSON
0
投稿

猜你喜欢

  • python人民币小写转大写辅助工具

    2022-02-10 15:28:19
  • Go语言中DateTime的用法介绍

    2024-04-27 15:31:55
  • python numpy 中linspace函数示例详解

    2021-12-12 01:47:59
  • 认识延迟时间为 0 的 setTimeout

    2008-04-04 16:37:00
  • 基于Python绘制520表白代码

    2021-06-10 08:14:44
  • 创建动态MSSQL数据库表

    2024-01-19 03:04:08
  • Debian中完全卸载MySQL的方法

    2024-01-25 19:25:34
  • pygame游戏之旅 创建游戏窗口界面

    2022-05-19 18:42:00
  • Python的Django框架中的表单处理示例

    2023-02-06 18:57:31
  • thinkphp3.x连接mysql数据库的方法(具体操作步骤)

    2023-11-22 20:04:41
  • sql 查询慢的原因分析

    2024-01-16 13:11:29
  • python math模块的基本使用教程

    2022-01-30 23:07:53
  • 一文详解Python灰色预测模型实现示例

    2023-05-11 01:45:21
  • python中opencv实现图片文本倾斜校正

    2023-08-27 11:07:03
  • Python中序列的修改、散列与切片详解

    2022-10-27 14:47:58
  • 如何解决tensorflow恢复模型的特定值时出错

    2023-12-22 14:59:36
  • vue实现PC端分辨率适配操作

    2024-05-09 09:29:57
  • Python面向对象程序设计之私有变量,私有方法原理与用法分析

    2022-04-17 01:37:50
  • 使用Python检测文章抄袭及去重算法原理解析

    2023-04-26 12:00:54
  • go语言学习之包和变量详解

    2024-04-26 17:29:27
  • asp之家 网络编程 m.aspxhome.com