Java 解析JSON时如何找到父Json节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20881154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:59:15  来源:igfitidea点击:

How to find parent Json node while parsing a JSON

javajsonHymansonjsonschema

提问by Optional

I am parsing a JSON Stream using Hymanson.

我正在使用 Hymanson 解析 JSON 流。

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

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

Consider following stream:

考虑以下流:

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

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).

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

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

我找不到任何 API 可以转到持有附加属性的 JsonNode 的父级。有没有办法实现这一目标?

Info:

信息:

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..

为什么我需要这个是我需要找到必须解决 $ref 的基本模式。为了找出基本模式,我需要知道它的父母的 id..

回答by StaxMan

Hymanson 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.

Hymanson JSON 树是单链接的,没有父链接。这样做的好处是减少了内存使用(因为可以共享许多叶级节点)和稍微更高效的构建,但缺点是无法上下遍历层次结构。

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

所以你需要自己跟踪它,或者使用你自己的树模型。

回答by Addo Solutions

Got it!

知道了!

@Override
public void serialize(ObjectId value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    JsonStreamContext parent = jgen.getOutputContext().getParent();
    // Win!
}

回答by Mubashar

I am not able to understand the exact context of your problem. But I was doing some similar thing where I had to find a property value and then change it if the value meets some sort of criteria.

我无法理解您问题的确切背景。但是我正在做一些类似的事情,我必须找到一个属性值,然后在该值满足某种标准时更改它。

To search the value node I used atfunction with JsonPointerand to go to Parent I used JsonPointer.headfunction and used atagain on root json node.

要搜索我使用at函数的值节点JsonPointer并转到父级,我使用了JsonPointer.head函数并at在根 json 节点上再次使用。

example as below

示例如下

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(JsonString);
JsonPointer valueNodePointer = JsonPointer.compile("/GrandObj/Obj/field");
JsonPointer containerPointer = valueNodePointer.head();
JsonNode parentJsonNode = rootNode.at(containerPointer);

//above is what you asked for as per my understanding
//following you can use to update the value node just for 
//the sake of completeness why someone really look for parent
//node
if (!parentJsonNode.isMissingNode() && parentJsonNode.isObject()) {
    ObjectNode parentObjectNode = (ObjectNode) parentJsonNode;
    //following will give you just the field name. e.g. if pointer is /grandObject/Object/field
    //JsonPoint.last() will give you /field 
    //remember to take out the / character 
    String fieldName = valueNodePointer.last().toString();
    fieldName = fieldName.replace(Character.toString(JsonPointer.SEPARATOR), StringUtils.EMPTY);
    JsonNode fieldValueNode = parentObjectNode.get(fieldName);
    if(fieldValueNode != null) {
        parentObjectNode.put(fieldName, 'NewValue');
    }
}