labbyfrost
labbyfrost

Reputation: 39

how to get 2 values from a nested Json string with jackson

I m using jackson library to deserialize from Json. I need to extract only 2 value from this json i.e. c1 and d1. I have used this code... I need to know the correct approach to overcome get c1 and d1 values...

My json

{"Alpha":{"A":{"B":{"C":{"c1":1234,c2:"abcd"},"D":{"d1":"xyz","d2":5678,"d3":"qwerty"},"E":[{"e1":456,"e2":"mnop"},{"e1":098,"e2":"qrst"}]}}},"X"{"x1":8098}}

ObjectMapper mapper = new ObjectMapper();
mainclass alphaobj = mapper.readValue(new File("C:\\employee.json"), mainclass.class);
System.out.println(alphaobj.A.B.C.getc1());

Upvotes: 1

Views: 1284

Answers (1)

StaxMan
StaxMan

Reputation: 116502

Maybe you should use Jackson Tree Model instead?

Something like:

JsonNode root = mapper.readTree(file);
int c1 = root.path("Alpha").path("A").path("C").path("C1").intValue();

and so on.

Upvotes: 1

Related Questions