From 5b099f34c3802b64325abb612cad105cab97a5d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 4 Jul 2022 23:28:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=B3=E9=94=AE=E5=AD=97?= =?UTF-8?q?=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lexical.go | 11 +++++++---- lexical_test.go | 7 ++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lexical.go b/lexical.go index 5f30d5d..526d0e0 100644 --- a/lexical.go +++ b/lexical.go @@ -43,7 +43,7 @@ type lexical struct { // Author : go_developer@163.com<白茶清欢> // // Date : 18:11 2022/7/4 -func (l *lexical) Parse(jsonData string) ([]lexicalNode, error) { +func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { // mt.Println(jsonData) if len(jsonData) < 2 { return nil, errors.New("input data is not json") @@ -101,8 +101,8 @@ func (l *lexical) Parse(jsonData string) ([]lexicalNode, error) { format = append(format, val) } l.lexicalResult = format - util.JSON.ConsoleOutput(l.lexicalResult) - return nil, nil + // util.JSON.ConsoleOutput(l.lexicalResult) + return l.lexicalResult, nil } // inputCharIsToken 输入字符是否为关键字 @@ -185,6 +185,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { // {"person": {"name":"zhangsan", "age": 18}} // {"person": {"work":true}} // {"person": {"like":1}} + // {"person_list": [{"like":1}]} if inputChar == objectRightToken && len(tmpStr) == 0 && ( // 对应 {}, [{}] (nil != preNode && preNode.Val == objectLeftToken) || @@ -193,7 +194,9 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { // 对应 {"person": {"name":"zhangsan"}} (nil != preNode && preNode.Val == objectRightToken)) || // 对应 {"person": {"work":true}} / {"person": {"like":1}} - (nil != preNode && preNode.Val == colonToken && (tmpStr == "number" || tmpStr == "bool")) { + (nil != preNode && preNode.Val == colonToken && (tmpStr == "number" || tmpStr == "bool")) || + // 对应 {"person_list": [{"like":1}]} + (nil != preNode && preNode.Val == listRightToken) { // } 是关键字 return true } diff --git a/lexical_test.go b/lexical_test.go index b64683a..2b81733 100644 --- a/lexical_test.go +++ b/lexical_test.go @@ -8,6 +8,7 @@ package filter import ( + "fmt" "testing" ) @@ -31,5 +32,9 @@ 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"}]}` - NewLexical(jsonData).Parse(jsonData) + r, _ := NewLexical(jsonData).Parse(jsonData) + for _, val := range r { + fmt.Print(val.Val) + } + fmt.Print("\n") }