0

I am able to parse json below:

{
    "jobId": "xxx",
    "jobName": "xxx",
    "jobInput": "xxx"
}

final ObjectMapper mapper = new ObjectMapper();
Map<?, ?> map = mapper.readValue(jsonString, Map.class);

I need to parse the below json string using jackson parser in java.

{
"Test1": {
    "jobId": "xxx",
    "jobName": "xxx",
    "jobInput": "xxx"
  },
"Test2": {
    "jobId": "xxx",
    "jobName": "xxx",
    "jobInput": "xxx"
  }
}
0

2 Answers 2

4

With Jackson, you can do the following:

ObjectMapper mapper = new ObjectMapper();
TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {};
Map<String, Object> data = mapper.readValue(json, typeRef);

If you prefer to use a custom class to hold the values instead of a Map, use:

ObjectMapper mapper = new ObjectMapper();
Data data = mapper.readValue(json, Data.class);
public class Data {

    @JsonProperty("Test1")
    private Job test1;

    @JsonProperty("Test2")
    private Job test2;

    // Default constructor, getters and setters
}
public class Job {

    private String jobId;

    private String jobName;

    private String jobInput;

    // Default constructor, getters and setters
}
Sign up to request clarification or add additional context in comments.

1 Comment

I did'nt want to use a custom class. the above solution works.Thanks, its very helpful.
0

How about just let Jackson parse a Map? Make the return type as Map<String, YourFirstDTO> and I think it will do.

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.