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") }