简体   繁体   中英

Parsing Nested JSON in java without know structure of JSON

I have a use case where i get a random jsonstring and variable name. I need to verify if that particular variable is present in that json, and if present fetch its value. For example, let us the json is as follows

{
   "a" : {
        "b":1,
        "c":2
   }
}

Along with above jsonString, say i get an input "ab" . Now I need to return 1. Is there any library to achieve this in java directly?

JsonPath is a library that provides the functionality you're after.

You will have to do some conversion between your input and the library's input.

As per your example, if your input is "ab":

String convertedInput = ".." + input
JsonPath.read(json, convertedInput)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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