简体   繁体   English

解析 json 返回与 Java 中的架构元素

[英]Parsing json returned with schema element in Java

I have a question about a json.我有一个关于 json 的问题。 Is the json below a standard of some kind. json 是否低于某种标准。 I can see why its preferable as the element names are written once and datatypes are included.我可以看到为什么它更可取,因为元素名称只写一次并且包含数据类型。 I cannot change how the JSON comes in, but is there a Java library that easily parses it?我无法更改 JSON 的输入方式,但是是否有一个 Java 库可以轻松解析它? Of course I can loop and loop but there must be something that parses it.当然我可以循环和循环,但必须有一些东西可以解析它。 I tried both with Jackson and org.json but both fail as it is an array at start.我尝试了 Jackson 和 org.json 但都失败了,因为它在开始时是一个数组。 Also "jq" parses it and I can get to the value "100" that I want as the COUNT but if the schema comes in different than directly referencing the array indexes would break. “jq”也解析它,我可以得到我想要作为 COUNT 的值“100”,但如果模式与直接引用数组索引不同,则会中断。

[
  {
    "header": {
      "queryId": "query_1590948930986",
      "schema": "`ROWKEY` STRING KEY, `USERID` STRING, `ENTITYTYPE` STRING, `ENTITYID` STRING, `DATESEARCHED` STRING, `COUNT` BIGINT"
    }
  },
  {
    "row": {
      "columns": [
        "6938bb62-50a1-4d0b-a113-1ecca1082763|+|n|+|09066437|+|2020-05-11",
        "6938bb62-50a1-4d0b-a113-1ecca1082763",
        "n",
        "09066437",
        "2020-05-11",
        100
      ]
    }
  }
]

in jq I can do this.在 jq 我可以做到这一点。 but will fail if schema element comes in different.但如果模式元素不同,则会失败。

| jq '.[1].row.columns[5]'

The returned JSON is valid.返回的 JSON 有效。 It is a JSON array containing two JSON Objects, each defining different properties.它是一个 JSON 数组,包含两个 JSON 对象,每个对象定义不同的属性。

In this specific case, the first element in the array contains metadata about the response, including the query id, (which may be included in server log lines or errors in the processing log), and the schema of the data returned in the row.columns array for the subsequent elements.在这种特定情况下,数组中的第一个元素包含有关响应的元数据,包括查询 ID(可能包含在服务器日志行或处理日志中的错误中),以及row.columns后续元素的row.columns数组。

For example, the value "6938bb62-50a1-4d0b-a113-1ecca1082763|+|n|+|09066437|+|2020-05-11" corresponds to the ROWKEY STRING KEY column.例如,值"6938bb62-50a1-4d0b-a113-1ecca1082763|+|n|+|09066437|+|2020-05-11"对应于ROWKEY STRING KEY列。

You can parse this data using Jackson.您可以使用 Jackson 解析此数据。 However, Jackson expects all elements in the array to share a common type.但是,Jackson 期望数组中的所有元素共享一个公共类型。 To deserialize using Jackson you will need a common base type, eg ResponseElement and two sub-types, eg HeaderElement and RowElement , with appropriate fields.要使用 Jackson 进行反序列化,您将需要一个通用的基本类型,例如ResponseElement和两个子类型,例如HeaderElementRowElement ,以及适当的字段。 You can then deserialize the JSON as a List<ResponseElement> .然后,您可以将 JSON 反序列化为List<ResponseElement>

More information on how to deserialize arrays with different element types can be found here:有关如何使用不同元素类型反序列化 arrays 的更多信息,请参见此处:

How to deserialize a JSON array of elements with different types and varying number of elements? 如何反序列化具有不同类型和不同数量元素的元素的 JSON 数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM