diff --git a/tree/generate.go b/tree/generate.go index 0789f99..5af3eb7 100644 --- a/tree/generate.go +++ b/tree/generate.go @@ -79,7 +79,7 @@ func (g *Generate) init() error { return err } // 获取值 - if jsonValue, err = g.getValue(startIndex); nil != err { + if jsonValue, startIndex, err = g.getValue(startIndex); nil != err { return err } fmt.Println(jsonKey, valueType, jsonValue, startIndex) @@ -102,6 +102,7 @@ func (g *Generate) getKey(startIndex int) (string, int, error) { startIndex++ continue } + if charStr == KeywordDoubleQuote && !hasStart { // 第一次遇见双引号 startIndex++ @@ -114,6 +115,14 @@ func (g *Generate) getKey(startIndex int) (string, int, error) { break } + if !hasStart { + if charStr == KeywordDoubleQuote || charStr == KeywordObjectStart || charStr == KeywordArrayStart || charStr == KeywordComma { + startIndex++ + continue + } + return "", startIndex, errors.New("parse key : format is invalid") + } + if charStr == KeywordEscapeSymbol { // 转义符 startIndex++ @@ -227,7 +236,7 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 12:09 2023/3/30 -func (g *Generate) getValue(startIndex int) (string, error) { +func (g *Generate) getValue(startIndex int) (string, int, error) { valArr := make([]string, 0) isStart := false for startIndex < len(g.jsonDataByte) { @@ -262,7 +271,7 @@ func (g *Generate) getValue(startIndex int) (string, error) { valArr = append(valArr, str) startIndex++ } - return strings.Join(valArr, ""), nil + return strings.Join(valArr, ""), startIndex, nil } // isObject 整体是否为对象