优化自动生成json #3
@@ -18,6 +18,8 @@ const (
|
|||||||
KeywordArrayEnd = "]"
|
KeywordArrayEnd = "]"
|
||||||
// KeywordColon 冒号
|
// KeywordColon 冒号
|
||||||
KeywordColon = ":"
|
KeywordColon = ":"
|
||||||
|
// KeywordComma 逗号
|
||||||
|
KeywordComma = ","
|
||||||
// KeywordDot .
|
// KeywordDot .
|
||||||
KeywordDot = "."
|
KeywordDot = "."
|
||||||
// KeywordDoubleQuote 双引号
|
// KeywordDoubleQuote 双引号
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ func (g *Generate) init() error {
|
|||||||
jsonKey string
|
jsonKey string
|
||||||
err error
|
err error
|
||||||
valueType string
|
valueType string
|
||||||
|
jsonValue string
|
||||||
)
|
)
|
||||||
|
|
||||||
// 获取类型
|
// 获取类型
|
||||||
@@ -77,7 +78,11 @@ func (g *Generate) init() error {
|
|||||||
if valueType, startIndex, err = g.getValueType(startIndex); nil != err {
|
if valueType, startIndex, err = g.getValueType(startIndex); nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(jsonKey, valueType, startIndex)
|
// 获取值
|
||||||
|
if jsonValue, err = g.getValue(startIndex); nil != err {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(jsonKey, valueType, jsonValue, startIndex)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -217,6 +222,49 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) {
|
|||||||
return keyValType, startIndex, nil
|
return keyValType, startIndex, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getValue 获取JSON值
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 12:09 2023/3/30
|
||||||
|
func (g *Generate) getValue(startIndex int) (string, error) {
|
||||||
|
valArr := make([]string, 0)
|
||||||
|
isStart := false
|
||||||
|
for startIndex < len(g.jsonDataByte) {
|
||||||
|
str := string(g.jsonDataByte[startIndex])
|
||||||
|
if str == KeywordSpace {
|
||||||
|
startIndex++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !isStart {
|
||||||
|
if str == KeywordArrayEnd || str == KeywordObjectEnd || str == KeywordComma {
|
||||||
|
startIndex++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isStart {
|
||||||
|
if str == KeywordDoubleQuote || str == KeywordArrayEnd || str == KeywordObjectEnd || str == KeywordComma {
|
||||||
|
// 值的拼接已结束
|
||||||
|
startIndex++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if str == KeywordDoubleQuote {
|
||||||
|
if isStart {
|
||||||
|
startIndex++
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
startIndex++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
isStart = true
|
||||||
|
valArr = append(valArr, str)
|
||||||
|
startIndex++
|
||||||
|
}
|
||||||
|
return strings.Join(valArr, ""), nil
|
||||||
|
}
|
||||||
|
|
||||||
// isObject 整体是否为对象
|
// isObject 整体是否为对象
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
jsonData := `{"n\\\\\\ame": "zhang", "age":17}`
|
jsonData := `{"name": "zhang", "age":17}`
|
||||||
g, err := New(jsonData)
|
g, err := New(jsonData)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
|
|||||||
Reference in New Issue
Block a user