Skip to content
Advertisement

How to store JSON in model – SpringBoot

Have JSON that looks like this:

"quote": [
    {
      "market": "APX Power NL Hourly",
      "date_applied": 1573599600000,
      "content": [
        {
          "gameId": "Order"...

I should save it to next model class:

public class GameDataResponse {    
    @JsonProperty(value = "gameId")
    public final String gameId;

    public GameDataResponse(
            String gameId, {
            notNull(gameId, "gameId must be set");
            this.gameId = gameId;
        } 
    public static GameDataResponse gameDataResponse(String gameId) {
        return new GameDataResponse(gameId);
    }
}

In gameDataResponse function I have to save gameId value from node to variable gameId but I am not sure if I am doing it right.

private GameDataResponse gameDataResponse(JsonNode node) {            
    return GameDataResponse.gameDataResponse(asString(node.get("gameId")));
}

With above code my variable is still empty.

Advertisement

Answer

It depends on what the variable node contains. Please try to debug the line asString(node.get("gameId")) to see what the variable node contains. This will let you know if this variable contains the JsonNode that you expect. Update after reading your comment: It looks like you need to navigate through the JsonNode to the corresponding field. The path should be as follows for the first gameId: “quote[0]/content[0]/gameId”. There are multiple ways to get to this value.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement