diff --git a/lexical.go b/lexical.go index 5dcd4fd..569c0ae 100644 --- a/lexical.go +++ b/lexical.go @@ -49,7 +49,8 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { tmpStr := "" for _, itemChar := range jsonData { currentChar := string(itemChar) - if l.inputCharIsToken(currentChar, tmpStr) { + tmpStrType := l.getTmpStrType(tmpStr) + if l.inputCharIsToken(currentChar, tmpStr, tmpStrType) { if currentChar == keyLeftRightToken { // 双引号计数 l.keyLeftRightTokenCnt++ @@ -72,20 +73,13 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { // 当前字符是空格, 只有在 "" 之间方才有效 , 关键字之间的空格, 忽略即可 continue } - tmpStr = tmpStr + currentChar - if (tmpStr == "\n" || tmpStr == "\t") && l.keyLeftRightTokenCnt%2 == 0 { - // 不在 "" 之间的 \n \t 无意义, 过滤掉 - tmpStr = "" + if (currentChar == "\n" || currentChar == "\t") && (l.keyLeftRightTokenCnt%2 == 0 || tmpStrType == "number" || tmpStrType == "bool") { + // 数字或者 bool 之后的 \n \t 无意义 , 不在 "" 之间也无意义 + continue } + tmpStr = tmpStr + currentChar } } - if len(tmpStr) > 0 { - l.lexicalResult = append(l.lexicalResult, &lexicalNode{ - Val: tmpStr, - IsToken: false, - }) - tmpStr = "" - } return l.lexicalResult, nil } @@ -95,7 +89,7 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 18:15 2022/7/4 -func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { +func (l *lexical) inputCharIsToken(inputChar string, tmpStr string, tmpStrType string) bool { tokenList := []string{ // list 类型起始 listLeftToken, @@ -161,7 +155,6 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { return true } - tmpStrType := l.getTmpStrType(tmpStr) // , 是关键字的场景 // {"name":"zhangsan", "age":"18"} // [{"name":"zhangsan", "age":"18"}, {"name":"zhangsan", "age":"18"}] diff --git a/lexical_test.go b/lexical_test.go index 2b81733..13bfce5 100644 --- a/lexical_test.go +++ b/lexical_test.go @@ -32,6 +32,7 @@ func Test_parseLexical(t *testing.T) { ] }` //jsonData = `{"name":"zhangsan","age":"18","extension":{"sex":"man","height":"180"},"teacher_list":[{"name":"t1","age":"11"},{"name":"t2","age":"12"}]}` + r, _ := NewLexical(jsonData).Parse(jsonData) for _, val := range r { fmt.Print(val.Val)