0

Probably a bit similar to this: deserializing json with arrays and also I'm following on from this: Jackson multiple objects and huge json files

The json I have is pretty big so simplified it goes something like this:

{ "foo"="bar", "x"="y",  "z"=[{"stuff"="stuff"}, {"more"="stuff"}, ...], ... }

I have code that looks like this:

for (Iterator<Map> it = new ObjectMapper().readValues(new JsonFactory().createJsonParser(in), Map.class); it.hasNext();) {
    doSomethingWith(it.next());
}

This works nicely at iterating over the objects in the file and I can get any value I like from the object I'm in. Works fine, but the array just comes back as an ArrayList of objects. So I have to do something like:

ArrayList z = (ArrayList) it.next().get("z");
for (Object o : z) {
    // Run mapper on o.
    // Do stuff.
}

I'm sure this would work, but it seems a bit messy to me. Is there a better way?

Community
  • 1
  • 1
Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
  • Oh. I figured it out already. I'll self-answer in 8 hours when I'm allowed :x But the gist is that the first run of the ObjectMapper already does everything for me and I just need to tell java that it's a Map, not an Object, and everything is groovy. – Tom Carrick May 05 '12 at 18:53

1 Answers1

0

Oh, whoops. Looks like the first run of the ObjectMapper does everything for me.

So I can just change my code like so:

ArrayList<Map> z = (ArrayList) it.next().get("z");
for (Map m : z) {
    // Run mapper on o.
    doSomethingWith(m.get("stuff");
}

and everything works splendidly.

Tom Carrick
  • 6,349
  • 13
  • 54
  • 78