Jackson - Extract Data without Brackets, Commas, and =?

1.6k Views Asked by At

I don't know a ton about Jackson, I'm just using it because I needed to share data from Python to Java. Anyway my code is pretty simple

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> data = mapper.readValue(new File(FileName), Map.class);
System.out.println(data.get("SomeInput"));

This is what I'm getting:

{Y=0.830168776371308, Z=0.16877637130801687, X=0.0010548523206751054}

I really just want to be able to use data to retrieve some type of data structure that holds the data without printing out the {} and the =, etc. Is there a method to do this?

I have a group of nodes, one node for each tag (such as ADP). I want to be able to give the ADP node 0.830... I can do this with the string, but it would involve some really annoying splitting of Strings. I'm assuming there must be an easy way to do this?

EDIT:

The data in the json file that I'm loading looks like this

{
    "!": {
        "X": 1.0
    }, 
    "$": {
        "X": 1.0
    }, 
    "&": {
        "X": 1.0
    }, 
    "/m": {
        "Y": 1.0
    }, 
 .....
2

There are 2 best solutions below

0
On BEST ANSWER
ObjectMapper mapper = new ObjectMapper(); 
Map<String,Object> data = mapper.readValue(new File(FileName), Map.class);
Map<String, Double> tags = (Map) data.get("SomeInput");

double value = 0;
for (String tag : tags.keySet()) {
    value = tags.get(tag); //Here I get all the data from the tags inside the input. E.g.: 0.830168776371308
    System.out.println(value); //It will print ADP, ADV and X values.
}
0
On

You already got a good answer on how to use Map. But for sake of completeness, there is another possibility that sometimes works even better, reading as a tree:

JsonNode root = mapper.readTree(new File(FileName));
JsonNode inputs = root.path("SomeInput");
String exclValue = inputs.path("!").asString();