-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconvert.go
51 lines (47 loc) · 971 Bytes
/
convert.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
46
47
48
49
50
51
package main
import (
"encoding/json"
"fmt"
"reflect"
ozlog "github.com/usthooz/oozlog/go"
)
// json2Struct 完成json转换
func (xj *xjson) json2Struct() *xjson {
Xkeyv = Xkeyvori
if xj.JSONTag {
Xkeyv = Xkeyvtag
}
if err := json.Unmarshal([]byte(xj.Msg), &xj.Parent); err != nil {
ozlog.Errorf("parse json failed:", err)
return nil
}
// start
xj.appendStr(fmt.Sprintf(Xbegin, xj.Name))
for k, v := range xj.Parent {
orik, k := xj.keyFilter(k)
tmptype := fmt.Sprintf(Xstr, reflect.TypeOf(v))
if tmptype == Xmap {
xj.subStruct(orik, v)
tmptype = k
}
if tmptype == Xlist {
xj.subList(orik, v)
tmptype = k
}
xj.appendStr(fmt.Sprintf(Xkeyv, k, tmptype, xj.MapTag[k]))
}
// end
xj.appendStr(Xend)
// out
if len(xj.OutType) == 0 {
xj.OutType = DefaultOutType
}
if xj.OutType == OutTypeForPrint {
xj.printStruct()
} else if xj.OutType == OutTypeForFile {
xj.writefileStruct()
}
// flush
xj.Flush()
return xj
}