0

I want to map the following json to a pojo in Java. In the snippet shown below, result is a Json object, whose value is another json object which is a map. I tried converting this to a Pojo, but it failed. The keys in the result map are dynamic, and I cannot guess them prior.

         final_result : 
         { 
              "result": 
                {
                    "1597696140": 70.32,
                    "1597696141": 89.12,
                    "1597696150": 95.32,
                }
         }

The pojo that I created is :


@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultData {
  Map<Long, Double> resultMap;

  public ResultData(Map<Long, Double> resultMap) {
    this.resultMap = resultMap;
  }

  public ResultData() {
  }

  @Override
  public String toString() {
    return super.toString();
  }
}

Upon trying to create the pojo using ObjectMapper :

ObjectMapper objectMapper = new ObjectMapper();
      ResultData resultData = objectMapper.readValue(resultData.getJSONObject("result").toString(), ResultData.class);

What am I possible doing wrong here ?

2
  • 1
    Well, you told Jackson to ignore unknown properties such as "1597696140". I'm also not sure Jackson is able to parse strings to longs out-of-the-box so you might need to add some custom deserialization here - or try with a Map<String, String>. Commented Aug 17, 2020 at 22:33
  • Thank you ! I did map to Map<Long, Double> and set the resultant map object to the pojo. Commented Aug 18, 2020 at 10:57

2 Answers 2

1

Assume, your JSON payload looks like below:

{
  "final_result": {
    "result": {
      "1597696140": 70.32,
      "1597696141": 89.12,
      "1597696150": 95.32
    }
  }
}

You can deserialise it to class:

@JsonRootName("final_result")
class ResultData {
    private Map<Long, Double> result;

    public Map<Long, Double> getResult() {
        return result;
    }

    @Override
    public String toString() {
        return result.toString();
    }
}

Like below:

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws IOException {
        File jsonFile = new File("./src/main/resources/test.json");

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
        ResultData resultData = mapper.readValue(jsonFile, ResultData.class);
        System.out.println(resultData);
    }
}

Above code prints:

{1597696140=70.32, 1597696141=89.12, 1597696150=95.32}
Sign up to request clarification or add additional context in comments.

Comments

0

Converting the JSONObject to Map and setting the map to the pojo field, solved the issue and didn't lead me to writing a custom deserializer.

Map<Long, Double> resultData = objectMapper.readValue(resultData.getJSONObject("result").toString(), Map.class);
FinalResultData finaResultData = new FinalResultData(resultData);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.