- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我开始为大学作业编写下面的代码,其中包括在 Common Lisp 中解析 json 字符串。我现在面临的主要问题是获取正确的子字符串\子序列以继续递归调用并解析字符串的其余部分。基本上主要思想是递归检查整个字符串。按照给定的语法,输入字符串应该是:
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 字符串。我无法真正解析的是整个原因,因为我不知道如何传递新子序列的正确索引:
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")))
最佳答案
您不应从输入中删除有助于确定接下来会发生什么的字符。 {name:Zaphod,heads:[[Head1],[Head2]]}
不干净,它是无效的 JSON。 JSON 中的所有键必须是字符串,所有字符串都包含在 ""
中。 Head1
在 JSON 中不是有效的东西。
干净地执行此操作的一种方法是首先标记字符串:
"{\"name\" : \"Zaphod\",\"heads\" : [[\"Head1\"], [\"Head2\"]]}"
产量
{
"name"
:
"Zaphod"
,
"heads"
:
[
[
"Head1"
]
,
[
"Head2"
]
]
}
然后 parse-json
函数查看第一个标记:如果它是一个字符串,它会产生一个字符串;如果它是一个数字,它会产生一个数字;如果它是一个 bool 值,它会产生那个 bool 值; ...如果是{
,它会调用parse-json-obj
;如果它是一个 [
,它调用 parse-json-array
。
Parse-json-obj
重复调用 parse-key-value
直到下一个标记是 ,而不是
,
。
Parse-key-value
解析字符串(否则出错),然后是 :
,然后调用 parse
获取值。
您可以通过将其余部分作为每个 parse* 函数的第二个值返回来跟踪您在标记列表中的位置。
关于json - 为解析 JSON 字符串的递归调用获取正确的子序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48068320/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!