简体   繁体   English

如何在没有模型的情况下找到JSON中的特定元素

[英]How to find specific element in JSON without model

I have a JSON data like this … 我有这样的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"
}
 ]
}

I am getting that JSON from a external call and I have to process that JSON for some element in that JSON. 我从外部调用中获取该JSON,并且必须对该JSON中的某些元素进行处理。

Like from the above structure suppose I want to get all data for tag “Brach Contact” for “North Brach” where "AllowExternal" is true. 与上述结构类似,假设我想获取“ North Brach”标签“ Brach Contact”的所有数据,其中“ AllowExternal”为true。

But I do not want to process the complete JSON and do not want to create the model object for the complete JSON structure in my code. 但是我不想处理完整的JSON,也不想在代码中为完整的JSON结构创建模型对象。

Is there any way I can do that with the JAVA , any external jar or something? 有什么办法可以用JAVA,任何外部jar或其他东西做到这一点?

My expected output is JSON structure for "Brach Contact" 我的预期输出是“ Brach Contact”的JSON结构

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

As input source is external , so there is a chance that they may change the JSON structure , so I what something which is independent of structure rather dependent on tag name. 由于输入源是外部的,因此它们可能会更改JSON结构,所以我有什么不依赖于结构而是取决于标记名的东西。 Although that is our next priority. 虽然这是我们的下一个优先事项。

Please note I am using Spring Boot with JAVA 8 请注意,我在JAVA 8中使用Spring Boot

Any one help on this… 任何人对此都有帮助...

You could take a look at JSONPath . 您可以看一下JSONPath

I found a nice introduction article here: https://www.baeldung.com/guide-to-jayway-jsonpath . 我在这里找到了一篇不错的介绍文章: https : //www.baeldung.com/guide-to-jayway-jsonpath

However, if you use this, you WILL need to put in the full path to the target element. 但是,如果使用此选项,则需要输入目标元素的完整路径。 But this will also parse the whole JSON, without you having to setup models and such. 但这也将解析整个JSON,而无需设置模型等。

If you don't want to parse the JSON then I suggest that you take a look at JSON Streaming Parsers . 如果您不想解析JSON,那么建议您看一下JSON流解析器

Jackson has a stream API: https://www.baeldung.com/jackson-streaming-api 杰克逊有一个流API: https//www.baeldung.com/jackson-streaming-api

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

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