From d424a2fd2081e48ebceda28b85ad9876770007ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 11 Nov 2021 18:01:18 +0800 Subject: [PATCH] =?UTF-8?q?list=E8=A7=A3=E6=9E=90=EF=BC=8C=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E7=A9=BAlist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/json2go/parser.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/tool/json2go/parser.go b/tool/json2go/parser.go index c845602..0af3d64 100644 --- a/tool/json2go/parser.go +++ b/tool/json2go/parser.go @@ -75,25 +75,29 @@ func (jg *JSON2GO) Parse(inputJSON string) (string, error) { func (jg *JSON2GO) parseArray(key string, parseResult gjson.Result) { // 先遍历一遍确认所有数据类型都相同 dataType := "" - for _, item := range parseResult.Array() { - if len(dataType) == 0 { - dataType = jg.getDataType(item) - continue - } - currentType := jg.getDataType(item) - if currentType != dataType { - if (dataType == "int64" && currentType == "float64") || (dataType == "float64" && currentType == "int64") { - dataType = "float64" + if len(parseResult.Array()) > 0 { + for _, item := range parseResult.Array() { + if len(dataType) == 0 { + dataType = jg.getDataType(item) continue } - // 不是所有数据类型都一致 - if len(key) == 0 { - jg.result += "interface{}" + currentType := jg.getDataType(item) + if currentType != dataType { + if (dataType == "int64" && currentType == "float64") || (dataType == "float64" && currentType == "int64") { + dataType = "float64" + continue + } + // 不是所有数据类型都一致 + if len(key) == 0 { + jg.result += "interface{}" + return + } + jg.append(jg.buildField("[]interface{}", key)) return } - jg.append(jg.buildField("[]interface{}", key)) - return } + } else { + dataType = "interface{}" } // 对象,重新 if dataType == "object" { @@ -101,6 +105,7 @@ func (jg *JSON2GO) parseArray(key string, parseResult gjson.Result) { r, _ := instance.Parse(parseResult.Array()[0].String()) dataType = strings.Replace(strings.Replace(r, "type", "", 1), "Automatic", "", 1) } + // 所有数据类型都一致 if len(key) == 0 { jg.append("[]" + dataType)