-
Notifications
You must be signed in to change notification settings - Fork 58
/
json.go
43 lines (38 loc) · 809 Bytes
/
json.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
41
42
43
package hltool
import (
"encoding/json"
"os"
)
// JSONFileToBytes 从json文件中转换为[]byte
func JSONFileToBytes(filepath string) ([]byte, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
fileinfo, err := file.Stat()
if err != nil {
return nil, err
}
buf := make([]byte, fileinfo.Size())
_, err = file.Read(buf)
if err != nil {
return nil, err
}
return buf, nil
}
// JSONBytesToStruct json []byte 转换为 struct
func JSONBytesToStruct(data []byte, structObj interface{}) error {
err := json.Unmarshal(data, structObj)
if err != nil {
return err
}
return nil
}
// JSONBytesToFile json []byte 写入文件
func JSONBytesToFile(data []byte, filepath string) error {
err := BytesToFile(data, filepath)
if err != nil {
return err
}
return nil
}