我尝试在java中解析递归json输入结构,如下面的格式,并尝试在另一个json中重写相同的结构。
同时我需要验证每个&解析时的每个json键/值。
{"动词":[{ " aaaa":" 30d","输入":" ed"," rel":1.0,&#34 ; id":" 80","口语":" en"," ct":" on" ," sps":null },{ " aaaa":" 31","输入":" cc"," rel":3.0,&#34 ; id":" 10","口语":" en"," ct":" off" ," sps":null },{ " aaaa":" 81","输入":" nn"," rel":3.0,&#34 ; id":" 60","口语":" en"," ct":" on" ," sps":null }]}
请建议我如何使用Jackson解析器JsonToken枚举来读取和编写未知的json内容。
答案 0 :(得分:0)
您可以使用JSON Schema验证您的输入。
找到数据格式的文档,但从我在这里可以看到的,架构将是这样的:
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"required": [ "Verbs" ],
"properties": {
"Verbs": { "type": "array", "items": { "$ref": "#/definitions/verb" } }
},
"definitions": {
"verb": {
"type": "object",
"required": [ "aaaa", "type", "rel", "id", "spoken", "ct", "sps" ],
"additionalProperties": false,
"properties": {
"aaaa": { "type": "string" },
"type": { "type": "string" },
"rel": { "type": "number" },
"id": { "type": "string", "pattern": "^[0-9]+$" },
"spoken": { "type": "string" },
"ct": { "enum": [ "on", "off" ] },
"sps": { "enum": [ null ] }
}
}
}
}
当您使用Jackson时,您可以使用this library来验证您的数据。
之后转换JSON可以通过创建新的JsonNode
来完成。