list解析,兼容空list

This commit is contained in:
白茶清欢 2021-11-11 18:01:18 +08:00
parent 4d83580f69
commit d424a2fd20

View File

@ -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)