我编写了一个处理JSON对象的程序。现在我想确认一下我是不是漏掉了什么。
是否存在所有允许的JSON结构组合的JSON示例?就像这样:
{
"key1" : "value",
"key2" : 1,
"key3" : {"key1" : "value"},
"key4" : [
[
"string1",
"string2"
],
[
1,
2
],
...
],
"key5" : true,
"key6" : false,
"key7" : null,
...
}
正如您在右边的http://json.org/所看到的,JSON的语法并不难,但是我有几个例外,因为我忘记了处理一些可能的结构组合。例如,数组中可以有“字符串、数字、对象、数组、真、假、空”,但我的程序在遇到异常之前无法处理数组中的数组。所以一切都很好,直到我得到了一个有效的JSON对象,其中包含数组。
我想用一个JSON对象测试我的程序(我正在寻找这个对象)。经过这个测试,我想确信我的程序毫无例外地处理了所有可能有效的JSON结构。
我不需要在5层左右筑巢。我只需要在嵌套深度2或最大3。与所有的基础类型嵌套所有允许的基础类型,在这个基本类型。
发布于 2016-08-06 09:10:22
您考虑过对象中的转义字符和对象吗?
{
"key1" : {
"key1" : "value",
"key2" : [
"String1",
"String2"
],
},
"key2" : "\"This is a quote\"",
"key3" : "This contains an escaped slash: \\",
"key4" : "This contains accent charachters: \u00eb \u00ef",
}
注:\u00eb
和\u00ef
为resp。查拉契斯
发布于 2016-08-06 08:29:07
选择支持json的编程语言。尝试加载您的json,如果失败,异常的消息是描述性的。
示例:
Python:
import json, sys;
json.loads(open(sys.argv[1]).read())
产生:
import random, json, os, string
def json_null(depth = 0):
return None
def json_int(depth = 0):
return random.randint(-999, 999)
def json_float(depth = 0):
return random.uniform(-999, 999)
def json_string(depth = 0):
return ''.join(random.sample(string.printable, random.randrange(10, 40)))
def json_bool(depth = 0):
return random.randint(0, 1) == 1
def json_list(depth):
lst = []
if depth:
for i in range(random.randrange(8)):
lst.append(gen_json(random.randrange(depth)))
return lst
def json_object(depth):
obj = {}
if depth:
for i in range(random.randrange(8)):
obj[json_string()] = gen_json(random.randrange(depth))
return obj
def gen_json(depth = 8):
if depth:
return random.choice([json_list, json_object])(depth)
else:
return random.choice([json_null, json_int, json_float, json_string, json_bool])(depth)
print(json.dumps(gen_json(), indent = 2))
https://stackoverflow.com/questions/38802145
复制相似问题