Giter VIP home page Giter VIP logo

gojson's Introduction

go-jsonObject

json字符串校验、反序列化JSON字符串成Golang对象、反序列化JSON字符串成Golang代码(JSON string validation, deserializing JSON strings into Golang objects, deserializing JSON strings into Golang code)

安装

  1. 使用go get
go get github.com/ChengjinWu/gojson
  1. 导入包
import (
	"github.com/ChengjinWu/gojson"
)

主要特性

  • json字符串校验(JSON string validation)
  • 反序列化JSON字符串成Golang对象(deserializing JSON strings into Golang objects)
  • 反序列化JSON字符串成Golang结构体、get、set方法(deserializing JSON strings into Golang code)

性能

测试文件

库\次数 10 100 1000 10000 100000 1000000
github.com/ChengjinWu/gojson 375µs 476µs 208µs 227.398ms 9.202ms 80.614ms
github.com/tidwall/gjson 37µs 208µs 2.141ms 2.141ms 171.448ms 1.56994s
github.com/widuu/gojson 2.714ms 208µs 227.398ms 1.985698s 18.592693s 3m11.946826s

示例

一、json字符串有效性校验

func Test_Validator(t *testing.T) {
	data := `{"id":524042,"name":"酷旅-mob-otv-2","male":true,"other":null}`
	err := gojson.CheckValid([]byte(data))
	if err != nil {
		fmt.Println(err)
	}
}

二、json字符串转Golang对象

func Test_json(t *testing.T) {
	data := `{"id":524042,"name":"酷旅-mob-otv-2","male":true,"other":null}`
	object, err := gojson.FromBytes([]byte(data))
	if err != nil {
		fmt.Println(err)
	} else {
		jsonBytes, _ := json.Marshal(object)
		fmt.Println(string(jsonBytes))
	}
}

三、json字符串内嵌数组转Golang对象

func Test_json_array_parse(t *testing.T) {
	data := `{
			  "id": [
				-524042.5,
				2312314444
			  ],
			  "name": "酷旅-mob-otv-2",
			  "male": true,
			  "other": null
			}`
	object, err := gojson.FromBytes([]byte(data))
	if err != nil {
		fmt.Println(err)
		return
	}
	idArray:=object.GetJsonObject("id").GetJsonArray()
	for i,v:=range idArray{
		fmt.Println(i,v.GetFloat64())
		fmt.Println(i,v.GetInt64())
	}
	name:=object.GetJsonObject("name").GetString()
	fmt.Println(name)
	male:=object.GetJsonObject("male").GetBool()
	fmt.Println(male)
	other:=object.GetJsonObject("other").GetInterface()
	fmt.Println(other)
}

四、json字符串转golang代码

func Test_json_array111(t *testing.T) {
	data := `{
	  "id": [
		-524042.5,
		231231444445555555
	  ],
	  "name": "酷旅-mob-otv-2",
	  "male": true,
	  "other": null
	}`
	object, err := gojson.FromBytes([]byte(data))
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(object.GetCoding("travel"))
	}
}

结果如下:

type Travel struct{
	Id    []float64   `json:"id"` 
	Male  bool        `json:"male"` 
	Name  string      `json:"name"` 
	Other interface{} `json:"other"` 
}
func (this *Travel) GetId() []float64 {
	if this == nil {
		return nil
	}
	return this.Id
}
func (this *Travel) SetId(id []float64) {
	if this == nil {
		return
	}
	this.Id = id
}
func (this *Travel) GetMale() bool {
	if this == nil {
		return false
	}
	return this.Male
}
func (this *Travel) SetMale(male bool) {
	if this == nil {
		return
	}
	this.Male = male
}
func (this *Travel) GetName() string {
	if this == nil {
		return ""
	}
	return this.Name
}
func (this *Travel) SetName(name string) {
	if this == nil {
		return
	}
	this.Name = name
}
func (this *Travel) GetOther() interface{} {
	if this == nil {
		return nil
	}
	return this.Other
}
func (this *Travel) SetOther(other interface{}) {
	if this == nil {
		return
	}
	this.Other = other
}

gojson's People

Contributors

chengjinwu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.