From 0521b77a204db1c9f7ac8bb0d1c2e723d74e326c Mon Sep 17 00:00:00 2001 From: huanghai <10402852@qq.com> Date: Wed, 26 Aug 2020 16:02:22 +0800 Subject: [PATCH] 'commit' --- .../TestFenXiJson/Struct/TBaseOrganization.go | 33 ++++++++++ dsBaseRpc/Test/TestFenXiJson/TestFenXiJson.go | 66 +++++++++++++++---- dsBaseRpc/go.mod | 1 + dsBaseRpc/go.sum | 2 + 4 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 dsBaseRpc/Test/TestFenXiJson/Struct/TBaseOrganization.go diff --git a/dsBaseRpc/Test/TestFenXiJson/Struct/TBaseOrganization.go b/dsBaseRpc/Test/TestFenXiJson/Struct/TBaseOrganization.go new file mode 100644 index 00000000..770945d7 --- /dev/null +++ b/dsBaseRpc/Test/TestFenXiJson/Struct/TBaseOrganization.go @@ -0,0 +1,33 @@ +package Struct + +type TBaseOrganization struct { + OrgCode string `json:"org_code"` + OrgId string `json:"org_id"` + Xxbxlxm string `json:"xxbxlxm"` + Address string `json:"address"` + BureauId string `json:"bureau_id"` + CreateTime string `json:"create_time"` + DelFlag float64 `json:"del_flag"` + OrgLng float64 `json:"org_lng"` + AreaCode string `json:"area_code"` + OrgType float64 `json:"org_type"` + ProvinceCode string `json:"province_code"` + Fddbrdh string `json:"fddbrdh"` + Fzr string `json:"fzr"` + LastUpdatedTime string `json:"last_updated_time"` + Lxdh string `json:"lxdh"` + OrgLat float64 `json:"org_lat"` + BUse float64 `json:"b_use"` + ParentId string `json:"parent_id"` + Szdcxlxm string `json:"szdcxlxm"` + CityCode string `json:"city_code"` + EduAssistType float64 `json:"edu_assist_type"` + OrgName string `json:"org_name"` + DistrictCode string `json:"district_code"` + MainSchoolType float64 `json:"main_school_type"` + SortId float64 `json:"sort_id"` + Xxjbzm string `json:"xxjbzm"` + Fddbr string `json:"fddbr"` + IdInt float64 `json:"id_int"` + MainSchoolId string `json:"main_school_id"` +} diff --git a/dsBaseRpc/Test/TestFenXiJson/TestFenXiJson.go b/dsBaseRpc/Test/TestFenXiJson/TestFenXiJson.go index 8467d1f6..8d190c1c 100644 --- a/dsBaseRpc/Test/TestFenXiJson/TestFenXiJson.go +++ b/dsBaseRpc/Test/TestFenXiJson/TestFenXiJson.go @@ -1,26 +1,68 @@ package main import ( + "bytes" + "dsBaseRpc/Utils/CommonUtil" "dsBaseRpc/Utils/FileUtil" "encoding/json" "fmt" + "github.com/iancoleman/strcase" "github.com/tidwall/gjson" + "os" + "path/filepath" + "reflect" + "strings" ) +//根据json对象字符串生成结构体 +func createStruct(jsonStr string, structName string) string { + m := make(map[string]interface{}) + err := json.Unmarshal([]byte(jsonStr), &m) + if err != nil { + fmt.Println("转化错误:", err) + } + var buffer bytes.Buffer + buffer.WriteString("type ") + buffer.WriteString(structName) + buffer.WriteString(" struct {\n") + for k, v := range m { + runes := []rune(k) + buffer.WriteString(strcase.ToCamel(string(runes))) //驼峰命名法 + buffer.WriteString(" ") + buffer.WriteString((reflect.TypeOf(v)).String()) + buffer.WriteString(" `json:\"") + buffer.WriteString(k) + buffer.WriteString("\"`") + buffer.WriteString("\n") + } + buffer.WriteString("}") + return buffer.String() +} + func main() { + //文件名称 + fileName := `e:\\TBaseOrganization.json` //1、读取json文件 - j := FileUtil.ReadFileContent("e:\\1.json") + j := FileUtil.ReadFileContent(fileName) result := gjson.Get(j, "datas") - for _, r1 := range result.Array() { - fmt.Println(r1.Get("data_id")) - fmt.Println(r1.Get("del_flag")) - re := r1.Get("data") - jsonStr := re.Str - var mymap map[string]interface{} - if err := json.Unmarshal([]byte(jsonStr), &mymap); err == nil { - for key, value := range mymap { - fmt.Println(key, ":", value) - } - } + + //2、生成JSON对应的struct + if len(result.Array()) == 0 { + fmt.Println("未找到数据,无法生成Struct~") + os.Exit(200) } + r1 := result.Array()[0] + fmt.Println(r1.Get("data_id")) + fmt.Println(r1.Get("del_flag")) + re := r1.Get("data") + jsonStr := re.Str + c := strings.Replace(filepath.Base(fileName), ".json", "", -1) + content := "package Struct\n" + createStruct(jsonStr, c) + parentPath, _ := os.Getwd() + parentPath = strings.Replace(parentPath, "\\", "/", -1) + //保存文件 + p1 := parentPath + "/Test/TestFenXiJson/Struct/" + c + ".go" + FileUtil.WriteContent(p1, content) + //格式化文件 + CommonUtil.Exec("gofmt", "-l", "-w", p1) } diff --git a/dsBaseRpc/go.mod b/dsBaseRpc/go.mod index 3e965995..a091aed1 100644 --- a/dsBaseRpc/go.mod +++ b/dsBaseRpc/go.mod @@ -41,6 +41,7 @@ require ( github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/hashicorp/serf v0.9.0 // indirect github.com/huandu/xstrings v1.3.1 // indirect + github.com/iancoleman/strcase v0.1.1 // indirect github.com/jmespath/go-jmespath v0.3.0 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/kavu/go_reuseport v1.5.0 // indirect diff --git a/dsBaseRpc/go.sum b/dsBaseRpc/go.sum index 47710f21..9dd48632 100644 --- a/dsBaseRpc/go.sum +++ b/dsBaseRpc/go.sum @@ -415,6 +415,8 @@ github.com/huandu/xstrings v1.3.0 h1:gvV6jG9dTgFEncxo+AF7PH6MZXi/vZl25owA/8Dg8Wo github.com/huandu/xstrings v1.3.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs= github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/iancoleman/strcase v0.1.1 h1:2I+LRClyCYB7JgZb9U0k75VHUiQe9RfknRqDyUfzp7k= +github.com/iancoleman/strcase v0.1.1/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb1-client v0.0.0-20190809212627-fc22c7df067e h1:txQltCyjXAqVVSZDArPEhUTg35hKwVIuXwtQo7eAMNQ=