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

时间:2015-06-15 14:40:42

标签: java json object jackson jackson-modules

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 个答案:

答案 0 :(得分:3)

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.
}

答案 1 :(得分:1)

您已经就如何使用Map得到了一个很好的答案。但是为了完整起见,还有另一种可能性,即有时候工作得更好,就像树一样:

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