0

我有一个这样的 JSON 数据……

{
{
   "School": "ABC School",
    "School Address": [
{
  "Office": "Road No 123"
},
{
  "Admin Office": "Road No 321"
},
{
  "Admission Office": "Road No 456"
}
],
 "School Brach": [
{
  "Brach name": "North Brach",
  "Brach Id ": "001",
  "Brach Contact": [
    {
      "Primary Phone": "12345676890",
      "AllowExternal":true
    },
    {
      "Primary Email": "xyz@school.com"
      "AllowExternal":true
    },
    {
      "Primary fax": "0123456789",
      "AllowExternal":false
    }
  ]
},
{
  "Brach name": "South Brach",
  "Brach Id ": "002"
},
{
  "Brach name": "West Brach",
  "Brach Id ": "003"
},
{
  "Brach name": "East Brach",
  "Brach Id ": "004"
}
 ]
}

我从外部调用中获取该 JSON,我必须为该 JSON 中的某些元素处理该 JSON。

就像上面的结构一样,假设我想为“North Brach”获取标签“Brach Contact”的所有数据,其中“AllowExternal”为真。

但我不想处理完整的 JSON,也不想在我的代码中为完整的 JSON 结构创建模型对象。

有什么办法可以用 JAVA 、任何外部 jar 或其他东西来做到这一点?

我的预期输出是“Brach Contact”的 JSON 结构

"Brach Contact": [
    {
      "Primary Phone": "12345676890",
      "AllowExternal":true
    },
    {
      "Primary Email": "xyz@school.com",
      "AllowExternal":true
    }
  ]

由于输入源是外部的,所以他们有可能会改变 JSON 结构,所以我什么东西是独立于结构而不是依赖于标签名称的。虽然这是我们的下一个优先事项。

请注意我正在使用带有 JAVA 8 的 Spring Boot

任何人都可以帮助...

4

1 回答 1

1

你可以看看JSONPath

我在这里找到了一篇不错的介绍文章:https ://www.baeldung.com/guide-to-jayway-jsonpath 。

但是,如果您使用它,您将需要输入目标元素的完整路径。但这也将解析整个 JSON,而无需设置模型等。

如果您不想解析 JSON,那么我建议您查看JSON Streaming Parsers

Jackson 有一个流 API:https ://www.baeldung.com/jackson-streaming-api

于 2019-05-04T06:25:19.453 回答