From 30eb5add688f0b356156e60cf39aaa54a9818e28 Mon Sep 17 00:00:00 2001 From: swxctx Date: Fri, 21 Dec 2018 14:44:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B1=8F=E5=B9=95=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- input.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/input.go b/input.go index 87fe5ee..7ee33cf 100644 --- a/input.go +++ b/input.go @@ -1,8 +1,12 @@ package main import ( + "encoding/json" "fmt" "os" + "reflect" + + "github.com/usthooz/oozlog/go" ) // readJsonToFile 从文件中读取json信息 @@ -21,6 +25,37 @@ func readJsonToFile(fileName string) string { break } json = `` + string(buf[:len]) + `` + ck := New("json2go", json) + ck.json2Struct() } return json } + +// 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 + } + 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])) + } + xj.printStruct() + xj.Flush() + return xj +}