解析JSON时如何找到父Json节点 [英] How to find parent Json node while parsing a JSON

查看:834
本文介绍了解析JSON时如何找到父Json节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jackson解析JSON流.

I am parsing a JSON Stream using Jackson.

我正在使用的API是ObjectMapper.readTree(..)

API that I am using is ObjectMapper.readTree(..)

考虑以下信息流:

{
  "type": "array",
  "items": {
      "id": "http://example.com/item-schema",
      "type": "object",
      "additionalProperties": {"$ref": "#"}
  }
}

现在,当我阅读其他属性时,我发现这里定义了一个"$ ref".现在要解析该引用,我需要转到其父级并找出id(以解析基本架构).

Now when I read additionalProperties, I figure out there is a "$ref" defined here. Now to resolve the reference, I need to go to its parent and figure out the id (to resolve the base schema).

我找不到任何API去拥有其他属性的JsonNode的父级.有什么办法可以做到这一点?

I can't find any API to go to parent of JsonNode holding additionalProperties. Is there a way I can achieve this?

信息:

为什么我需要这个,我需要找到必须解决$ ref的基本模式.为了弄清楚基础架构,我需要知道其父代的ID.

Why I need this is I need to find the base schema against which $ref has to be resolved. And to figure out base schema, I need to know the id of its parents..

推荐答案

Jackson JSON树是单链接的,没有父链接.这样做的好处是减少了内存使用量(因为可以共享许多叶级节点),并且构建效率稍高,但是不利之处在于无法遍历层次结构.

Jackson JSON Trees are singly-linked, there is no parent linkage. This has the benefit of reduced memory usage (since many leaf-level nodes can be shared) and slightly more efficient building, but downside of not being able to traverse up and down the hierarchy.

因此,您需要自己进行跟踪或使用自己的树模型.

So you will need to keep track of that yourself, or use your own tree model.

这篇关于解析JSON时如何找到父Json节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆