简体   繁体   中英

Converting a json file to a Java map, using a custom class type

So I am currently trying to convert a json file to a java map. I'm just using the following code which works fine as is:

ObjectMapper mapper = new ObjectMapper();
try {
    Map<String, Object> map = mapper.readValue(new File(
        "res/cache.json"), new TypeReference<Map<String, Object>>() {
    });

} catch (Exception e) {
    e.printStackTrace();
}

However, the problem is that I don't want the map to be of type Map<String, Object> but instead Map<String, NodeType> , where I have created a new static class as follows.

static class NodeType {
    // A map of all the nodes, for example '{0001 : node, 0002 : anotherNode}'
    public Map<String, Node> nodes; //Note: Node is another static class containing a map of values.

    public NodeType() {
        nodes = new HashMap<>();
    }
}

The solution I had was to replace new TypeReference<Map<String, Object>> with new TypeReference<Map<String, NodeType>> but I currently get an error where the structures of the json and the class don't quite match. To try and explain, the class takes each variable as a key/value in the json and then the map as another key/value mapping. Does anyone know how I could 'flatten' down the NodeType class to make both structures match.

Thanks.

Json file contents:

{
  "areas" : {
    "0001" : {
      "lightsOn" : false,
      "volume" : 30,
      "musicPlaying" : true,
      "videoPlaying" : false
    },
    "0002" : {
      "lightsOn" : false,
      "volume" : 15,
      "musicPlaying" : true,
      "videoPlaying" : false
    },
    "0003" : {
      "lightsOn" : true,
      "volume" : 60,
      "musicPlaying" : true,
      "videoPlaying" : false
    }
  },
  ...
}

Just to add on, when I parse the NodeType class through the Object Mapper, I get this:

{
  "nodes" : {
    "0002" : {
      "states" : {
        "volume" : 15,
        "musicPlaying" : true,
        "lightsOn" : false,
        "videoPlaying" : false
      }
    },
    "0003" : {
      "states" : {
        "volume" : 60,
        "musicPlaying" : true,
        "lightsOn" : true,
        "videoPlaying" : false
      }
    },
    "0001" : {
      "states" : {
        "volume" : 30,
        "musicPlaying" : true,
        "lightsOn" : false,
        "videoPlaying" : false
      }
    }
  }
}

EDIT: I feel like I might be on the right track with this tutorial - https://www.baeldung.com/jackson-map

Either you create a JSON representation that matches your file or you (de-)serialize the JSON manually.

The JSON representation could look similar to

public class JSONContent {

    private Map<String, AreasContent> areas;
}

public class AreasContent  {

    private boolean lightsOn;
    private int volume;
    private boolean musicPlaying;
    private boolean videoPlaying;
}

Usage:

JSONContent jsonContent = mapper.readValue(new File("res/cache.json"), JSONContent.class);

Edit:

Concerning your comment, you could try something like

public class JSONContent {

    private Map<String, Map<String, Object>> areas;
}

Usage as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM