完成简版value值获取

This commit is contained in:
白茶清欢 2023-03-30 12:26:17 +08:00
parent 9bbd781a1f
commit 7530e07e7a
3 changed files with 52 additions and 2 deletions

View File

@ -18,6 +18,8 @@ const (
KeywordArrayEnd = "]"
// KeywordColon 冒号
KeywordColon = ":"
// KeywordComma 逗号
KeywordComma = ","
// KeywordDot .
KeywordDot = "."
// KeywordDoubleQuote 双引号

View File

@ -55,6 +55,7 @@ func (g *Generate) init() error {
jsonKey string
err error
valueType string
jsonValue string
)
// 获取类型
@ -77,7 +78,11 @@ func (g *Generate) init() error {
if valueType, startIndex, err = g.getValueType(startIndex); nil != 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
}
@ -217,6 +222,49 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) {
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 整体是否为对象
//
// Author : go_developer@163.com<白茶清欢>

View File

@ -13,7 +13,7 @@ import (
)
func TestNew(t *testing.T) {
jsonData := `{"n\\\\\\ame": "zhang", "age":17}`
jsonData := `{"name": "zhang", "age":17}`
g, err := New(jsonData)
if nil != err {
fmt.Println(err.Error())