-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
45 lines (39 loc) · 852 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
44
45
package goutil
import (
"encoding/json"
"os"
"github.com/housemecn/goutil/file"
)
// JSONFileToBytes 从json文件中转换为[]byte
func JSONFileToBytes(filepath string) ([]byte, error) {
open, err := os.Open(filepath)
if err != nil {
return nil, err
}
fileInfo, err := open.Stat()
if err != nil {
return nil, err
}
buf := make([]byte, fileInfo.Size())
_, err = open.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 := file.BytesToFile(data, filepath)
if err != nil {
return err
}
return nil
}