mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
fix: 数据库实例删除等问题修复
This commit is contained in:
@@ -3,9 +3,8 @@ package jsonx
|
||||
import (
|
||||
"encoding/json"
|
||||
"mayfly-go/pkg/logx"
|
||||
"strings"
|
||||
|
||||
"github.com/buger/jsonparser"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// json字符串转map
|
||||
@@ -42,35 +41,35 @@ func ToStr(val any) string {
|
||||
//
|
||||
// @param fieldPath字段路径。如user.username等
|
||||
func GetStringByBytes(bytes []byte, fieldPath string) (string, error) {
|
||||
return jsonparser.GetString(bytes, strings.Split(fieldPath, ".")...)
|
||||
return gjson.GetBytes(bytes, fieldPath).String(), nil
|
||||
}
|
||||
|
||||
// 根据json字符串获取对应字段路径的string类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.username等
|
||||
func GetString(jsonStr string, fieldPath string) (string, error) {
|
||||
return GetStringByBytes([]byte(jsonStr), fieldPath)
|
||||
return gjson.Get(jsonStr, fieldPath).String(), nil
|
||||
}
|
||||
|
||||
// 根据json字节数组获取对应字段路径的int类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.age等
|
||||
func GetIntByBytes(bytes []byte, fieldPath string) (int64, error) {
|
||||
return jsonparser.GetInt(bytes, strings.Split(fieldPath, ".")...)
|
||||
return gjson.GetBytes(bytes, fieldPath).Int(), nil
|
||||
}
|
||||
|
||||
// 根据json字符串获取对应字段路径的int类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.age等
|
||||
func GetInt(jsonStr string, fieldPath string) (int64, error) {
|
||||
return GetIntByBytes([]byte(jsonStr), fieldPath)
|
||||
return gjson.Get(jsonStr, fieldPath).Int(), nil
|
||||
}
|
||||
|
||||
// 根据json字节数组获取对应字段路径的bool类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.isDeleted等
|
||||
func GetBoolByBytes(bytes []byte, fieldPath string) (bool, error) {
|
||||
return jsonparser.GetBoolean(bytes, strings.Split(fieldPath, ".")...)
|
||||
return gjson.GetBytes(bytes, fieldPath).Bool(), nil
|
||||
}
|
||||
|
||||
// 根据json字符串获取对应字段路径的bool类型值
|
||||
|
||||
@@ -3,8 +3,6 @@ package jsonx
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/buger/jsonparser"
|
||||
)
|
||||
|
||||
const jsonStr = `{
|
||||
@@ -36,7 +34,7 @@ func TestGetString(t *testing.T) {
|
||||
// val, err := GetString(jsonStr, "username1")
|
||||
|
||||
// 含有数组的
|
||||
val, err := GetString(jsonStr, "person.avatars.[0].url")
|
||||
val, err := GetString(jsonStr, "person.avatars.0.url")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("error: ", err.Error())
|
||||
@@ -50,60 +48,3 @@ func TestGetInt(t *testing.T) {
|
||||
val2, _ := GetInt(jsonStr, "person.github.followers")
|
||||
fmt.Println(val, ",", val2)
|
||||
}
|
||||
|
||||
// 官方demo
|
||||
func TestJsonParser(t *testing.T) {
|
||||
data := []byte(jsonStr)
|
||||
// You can specify key path by providing arguments to Get function
|
||||
jsonparser.Get(data, "person", "name", "fullName")
|
||||
|
||||
// There is `GetInt` and `GetBoolean` helpers if you exactly know key data type
|
||||
jsonparser.GetInt(data, "person", "github", "followers")
|
||||
|
||||
// When you try to get object, it will return you []byte slice pointer to data containing it
|
||||
// In `company` it will be `{"name": "Acme"}`
|
||||
jsonparser.Get(data, "company")
|
||||
|
||||
// If the key doesn't exist it will throw an error
|
||||
var size int64
|
||||
if value, err := jsonparser.GetInt(data, "company", "size"); err == nil {
|
||||
size = value
|
||||
fmt.Println(size)
|
||||
}
|
||||
|
||||
// You can use `ArrayEach` helper to iterate items [item1, item2 .... itemN]
|
||||
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
|
||||
fmt.Println(jsonparser.Get(value, "url"))
|
||||
}, "person", "avatars")
|
||||
|
||||
// Or use can access fields by index!
|
||||
jsonparser.GetString(data, "person", "avatars", "[0]", "url")
|
||||
|
||||
// You can use `ObjectEach` helper to iterate objects { "key1":object1, "key2":object2, .... "keyN":objectN }
|
||||
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
|
||||
fmt.Printf("Key: '%s'\n Value: '%s'\n Type: %s\n", string(key), string(value), dataType)
|
||||
return nil
|
||||
}, "person", "name")
|
||||
|
||||
// The most efficient way to extract multiple keys is `EachKey`
|
||||
|
||||
paths := [][]string{
|
||||
[]string{"person", "name", "fullName"},
|
||||
[]string{"person", "avatars", "[0]", "url"},
|
||||
[]string{"company", "url"},
|
||||
}
|
||||
|
||||
jsonparser.EachKey(data, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
|
||||
switch idx {
|
||||
case 0: // []string{"person", "name", "fullName"}
|
||||
{
|
||||
}
|
||||
case 1: // []string{"person", "avatars", "[0]", "url"}
|
||||
{
|
||||
}
|
||||
case 2: // []string{"company", "url"},
|
||||
{
|
||||
}
|
||||
}
|
||||
}, paths...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user