Update: I should have mentioned this right off the bat: I first considered a Java/JSON mapping framework, but my manager does not want me adding any more dependencies to the project, so that is out as an option. The JSON-Java jar is already on our classpath, so I could use that, but still not seeing the forest through the trees on how it could be used.
My Java program is being handed JSON of the following form (althought the values will change all the time):
{"order":{"booze":"1","handled":"0","credits":"0.6",
"execute":0,"available":["299258"],"approved":[],
"blizzard":"143030","reviewable":["930932","283982","782821"],
"units":"6","pending":["298233","329449"],"hobbit":"blasphemy"}}
I'm looking for the easiest, efficient, surefire way of cherry-picking specific values out of this JSON string and aggregating them into a List<Long>
.
Specifically, I'm looking to extract-and-aggregate all of the "ids", that is, all the numeric values that you see for the available
, approved
, reviewable
and pending
fields. Each of these fields is an array of 0+ "ids". So, in the example above, we see the following breakdown of ids:
available
: has 1 id (299258)approved
: has 0 idsreviewable
: has 3 ids (930932, 283982, 782821)pending
: has 2 ids (298233, 329449)
I need some Java code to run and produce a List<Long>
with all 6 of these extracted ids, in no particular order. The ids just need to make it into the list.
This feels like an incredibly complex, convoluded regex, and I'm not even sure where too begin. Any help at all is enormously appreciated. Thanks in advance.
See Question&Answers more detail:os