From f760ab057ab013937d6a39cc77ea98bbaeba93bf 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, 30 Mar 2023 13:59:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9F=BA=E7=A1=80=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E8=A7=A3=E6=9E=90=E8=8E=B7=E5=8F=96=E5=80=BC=E7=9A=84?= =?UTF-8?q?BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tree/generate.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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 整体是否为对象