Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading multiple JSON Objects from a single file into Java with Jackson

So I've done my best to research similar issues like this, but I'm fairly new to Java and JSON, so I apologize if this is redundant. I'm working on a simple text based RPG for my Java class this semester, and I want to store character information and location information in either XML or JSON. I've given both a try, and seem to be getting farther with JSON and Jackson. I've written some tester code to try this out before implementing in my game, but basically I want each game "location" to have an integer ID#, some string information, and a list of exitNodes (which will eventually correspond to other locations in the 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'm guessing I could organize my JSON a bit better, but ideally I want to be able to grab an object by it's number, and get the "enterInfo" and "exitNodes" back. I've spent hours trying different things without success, but the best I can do is grab the whole JSON document as a JsonNode which just gives me the entire structure, but not an individual object.

Here is a test class (minus constructor/getters/setters/methods) I'm trying to get the JSON into:

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

And here is what I have working so far, I realize that I'm not even close to achieving my goal here, but I'm out of ideas and the documentation is starting to get confusing, so I've removed my attempts to isolate one of the objects in the JSON file and instantiate an "ObjectTest" object:

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 matters, but here is what prints out:

{"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 being able to create an "ObjectTest" object with the values from my JSON. Again, sorry if this is a duplicate, I've read through numerous posts already, but I'm new here...

EDIT: I'm realizing now that my JSON file should probably be organized more like this:

{"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 need to then create a list of objects defined by the JSON?

like image 714
purpleSun Avatar asked Apr 30 '13 23:04

purpleSun


1 Answers

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]
}]

Reference

EDIT
Forgot to say that the fields need to have setters or to be public

like image 124
joaonlima Avatar answered Nov 02 '22 22:11

joaonlima