获取解析JSON字符串的递归调用的正确子序列

问题描述 投票:1回答:1

我开始编写下面的代码,用于大学的家庭作业,包括解析Common Lisp中的json-string。我现在面临的主要问题是获得正确的子字符串\子序列以继续递归调用并解析字符串的其余部分。基本上主要的想法是递归检查整个字符串。按照给定的语法,输入字符串应为:

    1) "{\"nome\" : \"arturo\" , \"cognome\" : \"durone\" , \"età\" : \"22\" , \"corso\" : \"informatica\"}"
    2) "{\"name\" : \"Zaphod\",\"heads\" : [[\"Head1\"], [\"Head2\"]]}"
    3) "{\"name\" : \"Zaphod\",{property:value, property : [1,2,3] }
    4) "[1,2,3]"

基本上我删除任何空格和任何\“从字符串得到一个干净的字符串"{name:Zaphod,heads:[[Head1],[Head2]]}",在这里我检查':'的位置,得到从0到((position ":")-1)的子序列,并在第二部分相同,但问题来了,当我有传递给递归调用,因为我不知道如何传递字符串的正确索引。

我试图检查函数在输出中给出的新列表的任何元素的长度,但它不起作用/帮助,因为字符串被拆分,并且没有来自初始输入的空格和\"字符。你能帮我找一个方法来解析递归方法后的其余json字符串吗?

> main function 
(defun j-obj (str) 
 (cond ((correct_form str)
        `(json-obj-aux(revome_braces (remove_backslash(remove_space str)))))))`

> aux function that thru a recursive call analize the whole string 
(defun json-obj-aux (str)
 (cond ((= (length str) 0)nil)
       ((cons (aux_control str)nil))))
            ;   (json-obj-aux (subseq (shorter str)(length (aux_control (shorter str)))
                                ;                          (length (shorter str))))))))
> check the whole string , splitting once it finds ":"
(defun aux_control (str) 
   (cons (subseq str 0 (search ":" str))(check_type (subseq str (+ (search ":" str) 1) (length str)))))

(defun check_type (str)
  (cond ((equal (subseq str 0 1) "{")(obj_c str))
        ((equal (subseq str 0 1) "[")(cons (obj_array (remove_braces str))nil))
        (t (cons (subseq str 0 (search "," str))nil))))


(defun obj_c (str)
 "{")

(defun obj_array (str)
  (cond ((= 0 (length str))nil)
        ((null (search "," str))(cons (subseq str (+ (search "[" str)1)(- (length str)1))nil))
        ((and (null (search "[" str))(null (search "," str)))(cons str nil))
        ((null (search "[" str))(cons (subseq str 0 (search "," str))
                                      (obj_array (subseq str (+ (search "," str) 1)))))
        ((cons (subseq str (+ (search "[" str) 1)(search "]" str))
              (obj_array (subseq str (+ (search "," str) 1)))))))

(defun remove_space (str)
 (cond ((= 0 (length str))nil)
       ((concatenate 'string (remove_aux str) (remove_space(subseq str 1))))))

(defun remove_aux (str)
 (cond ((equal (subseq str 0 1) " ")"")
       ((concatenate 'string (subseq str 0 1) ""))))


(defun remove_backslash (str)
  (cond ((= 0 (length str))nil)
        ((concatenate 'string (remove_slash str)(remove_backslash(subseq str 1))))))

(defun remove_slash (str)
  (cond ((equal (subseq str 0 1) "\"")"")
        ((concatenate 'string (subseq str 0 1) ""))))

(defun remove_braces (str)
  (subseq str 1 (- (length str) 1)))


(defun shorter (str)
  (subseq str 1 (length str)))

这是我到目前为止所得到的,这并非完全错误,因为我可以解析部分json-string。我无法真正解析的是整个因素我不知道如何传递新子序列的正确索引:

    CL-USER 1 >  (j-obj "{\"name\" : \"Zaphod\",\"heads\" : [[\"Head1\"], [\"Head2\"]]}")
    (("name" "Zaphod"))


    CL-USER 2 > (j-obj "{\"heads\" : [[\"Head1\"], [\"Head2\"]]}")
    (("heads" ("Head1" "Head2")))

正确的输出应该是:

(("name" "Zaphod")("heads" ("Head1" "Head2")))
json lisp common-lisp clisp gnu-common-lisp
1个回答
2
投票

您不应该从输入中删除有助于确定接下来会发生什么的字符。 {name:Zaphod,heads:[[Head1],[Head2]]}不干净,它是无效的JSON。 JSON中的所有键必须是字符串,所有字符串都包含在""中。 Head1在JSON中不是一个有效的东西。

干净利落的一种方法是先对字符串进行标记:

"{\"name\" : \"Zaphod\",\"heads\" : [[\"Head1\"], [\"Head2\"]]}"

产量

{
"name"
:
"Zaphod"
,
"heads"
:
[
[
"Head1"
]
,
[
"Head2"
]
]
}

然后parse-json函数查看第一个标记:如果它是一个字符串,它会产生一个字符串;如果是数字,则产生一个数字;如果它是布尔值,则产生该布尔值; ......如果它是{,它叫parse-json-obj;如果它是[,它会调用parse-json-array

Parse-json-obj反复呼叫parse-key-value直到下一个标记是},而不是,

Parse-key-value解析一个字符串(否则出错),然后是:,然后调用parse作为值。

您可以通过将其余部分作为每个解析*函数的第二个值返回来跟踪您在令牌列表中的位置。

© www.soinside.com 2019 - 2024. All rights reserved.