Reading multiple JSON objects from a single file in Java using Jackson

So I did my best to investigate similar problems like this, but I'm pretty new to Java and JSON, so I apologize if this is redundant. I am working on a plain text RPG for my Java class this semester and I want to store character information and location information in XML or JSON. I gave it a try and it seems to go farther and farther with JSON and Jackson. I wrote a tester code to try this before implementing it in my game, but basically I want each location game to have an integer ID, some string information and an exitNodes list (which will eventually correspond to other locations in Game). Here is an example of my JSON:

{ "1":{ "enterInfo":"Test Location Information", "exitNodes":[2,3] }, "2":{ "enterInfo":"More Test Location Info", "exitNodes":[4,5] }, "3":{ "enterInfo":"More Test Location Info", "exitNodes":[6,7] } } 

I suppose I could organize my JSON a little better, but ideally I want to be able to grab the object by its number and return "enterInfo" and "exitNodes". I spent several hours trying different things without success, but the best I can do is grab the entire JSON document as a JsonNode, which just gives me the whole structure, but not a separate object.

Here is a test class (minus the constructor / getters / seters / methods) I am trying to get JSON in:

 public class ObjectTest{ int id; String enterInfo; int[] exitNodes; } 

And here’s what I’m still working on, I understand that I’m not even close to achieving my goal here, but I don’t have any ideas and the documentation starts to get confused, so I deleted my trying to isolate one of the objects in the JSON file and create an instance of the object ObjectTest:

 package jacksontester; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; public class JackSONTester { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); JsonNode localeTemp = mapper.readTree(new File("src/jacksontester/TestMeJSON.json")); System.out.println(localeTemp); } } 

Not that it really mattered, but here's what prints:

 {"1":{"enterInfo":"Test Location Information","exitNodes":[2,3]},"2":{"enterInfo":"More Test Location Info","exitNodes":[4,5]},"3":{"enterInfo":"More Test Location Info","exitNodes":[6,7]}} 

What I want to achieve is to create an ObjectTest object with values ​​from my JSON. Again, sorry if this is a duplicate, I already read a lot of posts, but I'm new here ...

EDIT: I understand that my JSON file should probably be more organized as follows:

 {"locations":[ { "id":0, "enterInfo":"Test Location Information", "exitNodes":[1,2] }, { "id":1, "enterInfo":"More Test Location Info", "exitNodes":[2,3] }, { "id":2, "enterInfo":"More Test Location Info", "exitNodes":[4,5] } ] } 

I would then need to create a list of objects defined by JSON?

+7
source share
1 answer

This should solve it for you:

 import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import java.io.File; import java.io.IOException; import java.util.List; public class JacksonTester { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); List<ObjectTest> myObjects = mapper.readValue(jsonFile(), new TypeReference<List<ObjectTest>>(){}); System.out.println(myObjects); } private static File jsonFile() { return new File("src/main/resources/test.json"); } } 

Using this as JSON:

 [{ "id":0, "enterInfo":"Test Location Information", "exitNodes":[1,2] }, { "id":0, "enterInfo":"Test Location Information", "exitNodes":[1,2] }] 

Link

EDIT
I forgot to say that the fields must have setters or be public.

+9
source

All Articles