【问题标题】:Jackson Parser for recursively parsing unknown input structure用于递归解析未知输入结构的 Jackson Parser
【发布时间】:2014-03-01 19:21:18
【问题描述】:

我正在尝试像下面的格式一样在 java 中递归解析 json 输入结构,并尝试在另一个 json 中重写相同的结构。

同时,我需要在解析时验证每个 json 键/值。

{“动词”:[{ “aaaa”:“30d”,“type”:“ed”,“rel”:1.0,“id”:“80”,“spoken”:“en”,“ct”:“on”,“sps”:空值 },{ “aaaa”:“31”,“type”:“cc”,“rel”:3.0,“id”:“10”,“spoken”:“en”,“ct”:“off”,“sps”:空值 },{ “aaaa”:“81”,“type”:“nn”,“rel”:3.0,“id”:“60”,“spoken”:“en”,“ct”:“on”,“sps”:空值 }]}

请建议我如何使用 Jackson 解析器 JsonToken 枚举来读取和写入未知的 json 内容。

【问题讨论】:

  • 您可以使用 JSON Schema 来验证您的 JSON。完全转换是什么意思?

标签: java json parsing recursion jackson


【解决方案1】:

您可以使用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 来完成,例如。

【讨论】:

  • Json 输入数据结构在解析之前是未知的,所以我无法在不知道数据结构的情况下创建模式
  • 你的意思是根本不知道?难道你连隐含的可能结构都不知道吗?
猜你喜欢
  • 2014-04-02
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
相关资源
最近更新 更多