Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Struct
转换
Struct
方法用于将整个Json
包含的数据内容转换为指定的数据格式或者对象。
Code Block | ||
---|---|---|
| ||
data := ` { "count" : 1, "array" : ["John", "Ming"] }` if j, err := gjson.DecodeToJson(data); err != nil { panic(err) } else { type Users struct { Count int Array []string } users := new(Users) if err := j.StructScan(users); err != nil { panic(err) } fmt.Printf(`%+v`, users) } // Output: // &{Count:1 Array:[John Ming]} |
GetStruct
Struct
转换
Get*
方法用于获得指定层级的节点数据,并将该数据转换为指定的结构体对象。
Code Block | ||
---|---|---|
| ||
func main() { data := `{ "users" : { "count" : 1, "array" : ["John", "Ming"] } }` if j, err := gjson.DecodeToJson(data); err != nil { panic(err) } else { type Users struct { Count int Array []string } users := new(Users) if err usersVar := j.GetStructGet("users", users); err != nil { panic(err) } usersVar.Scan(users) fmt.Printf(`%+v`, users) } // Output: // &{Count:1 Array:[John Ming]} } |
Panel | ||
---|---|---|
| ||
|