youbbs avatar

把JSON数据转为Go struct

🕟 by youbbs

一个在线懒人工具,把json 转换为go struct 结构,效果如下图

前往试试 JSON-to-Go https://mholt.github.io/json-to-go/

💘 相关文章

评论

共1条关于"把JSON数据转为Go struct"的评论

getkokomi avatar
#1 getkokomi 回复

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//UserUpdate 更新用户
func UserUpdate(_db *db.DB, obj User) error {
	jb, _ := json.Marshal(obj)
	return _db.Hset("user", db.I2b(obj.ID), jb)
}

//UserGetByID  通过ID获取用户信息
func UserGetByID(_db *db.DB, uid uint64) (User, error) {
	obj := User{}
	rs := _db.Hget("user", db.I2b(uid))
	if rs.State == "ok" {
		json.Unmarshal(rs.Data[0], &obj)
		return obj, nil
	}
	return obj, errors.New(rs.State)
}

//UserGetByName 通过name获取用户信息
func UserGetByName(_db *db.DB, name string) (User, error) {
	obj := User{}
	rs := _db.Hget("user_name2uid", []byte(name))
	if rs.State == "ok" {
		rs2 := _db.Hget("user", rs.Data[0])
		if rs2.State == "ok" {
			json.Unmarshal(rs2.Data[0], &obj)
			return obj, nil
		}
		return obj, errors.New(rs2.State)
	}
	return obj, errors.New(rs.State)
}

//UserGetIDByName 通过name获取用户ID
func UserGetIDByName(_db *db.DB, name string) string {
	rs := _db.Hget("user_name2uid", []byte(name))
	if rs.State == "ok" {
		return db.B2ds(rs.Data[0])
	}
	return ""
}

写一条评论

Based on Golang + fastHTTP + sdb | go1.16.3 Processed in 1ms