mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-05 23:16:50 +08:00
- Optimized the validation of the result type in the jsonUtil
package (#9)
- Fixed the validation error of passing an empty string for the data type in `JsonToStruct` - 优化了 `jsonUtil` 包中result类型的验证 - 修复了`JsonToStruct`中数据类型传入空字符串的校验错误
This commit is contained in:
@@ -2,6 +2,7 @@ package jsonUtil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -10,6 +11,10 @@ import (
|
||||
// JsonToStruct Parses JSON into a specified structure pointer
|
||||
// 将JSON解析为指定的结构体指针
|
||||
func JsonToStruct(jsonData string, result interface{}) error {
|
||||
if reflect.ValueOf(result).Kind() != reflect.Pointer || reflect.ValueOf(result).IsNil() {
|
||||
return errors.New("the argument to Result must be a non-nil pointer")
|
||||
}
|
||||
|
||||
var data map[string]interface{}
|
||||
err := json.Unmarshal([]byte(jsonData), &data)
|
||||
if err != nil {
|
||||
@@ -104,11 +109,6 @@ func JsonToStruct(jsonData string, result interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//func isJSON(jsonStr string) bool {
|
||||
// var raw json.RawMessage
|
||||
// return json.Unmarshal([]byte(jsonStr), &raw) == nil
|
||||
//}
|
||||
|
||||
func convertToJSONString(data map[string]interface{}) string {
|
||||
jsonBytes, _ := json.Marshal(data)
|
||||
return string(jsonBytes)
|
||||
|
Reference in New Issue
Block a user