skip to Main Content

What would be the Java POJO class for below "datapoints"?
datapoints are not constants their values and quantity and counts can increase or decrease.

This below is the output I’m getting from my code where I get backslash.

[
    {
        "id": "346id71",
        "arg": {
            "test1": "pass",
            "task": "true",
            "direction": "right"
        },
        "datapoints": "{"1694473620":0.0008208249347420499,"1694509620":0.0008401434897869077,"1694545620":0.0008438824499876583,"1694581620":0.0008171293069653031,"1694617620":0.0008596749524634415,"1694653620":0.0008368097665295172}"
    },
    {
        "id": "346id72",
        "arg": {
            "test1": "pass",
            "task": "true",
            "direction": "left"
        },
        "datapoints": "{"1694473620":0.0008208249347420499,"1694509620":0.0008401434897869077,"1694545620":0.0008438824499876583,"1694581620":0.0008171293069653031,"1694617620":0.0008596749524634415,"1694653620":0.0008368097665295172}"
    }
]

This below is the output needed and but I’m getting backslash in "datapoints" as above

[
    {
        "id": "346id71",
        "arg": {
            "test1": "pass",
            "task": "true",
            "direction": "right"
        },
        "datapoints": {
            "1680012000": 0.10124447461480172,
            "1680048000": 8.39834838777665454,
            "1680084000": 8.33850837187659432,
            "1680120000": 8.35752337464636549
            }
    },
    {
        "id": "346id72",
        "arg": {
            "test1": "pass",
            "task": "true",
            "direction": "left"
        },
        "datapoints": {
            "1680012000": 0.123233,
            "1680048000": 3.567384,
            "1680084000": 4.476530,
            "1680120000": 0.0,
            "1680156000": 0.0
          }
    }
]

I tried a lot but couldn’t figure out proper solution to remove backslash.

this is my model class


public class Response {

    private String id;
    private Arg arg;
    private String datapoints;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Arg getArg() {
        return arg;
    }
    public void setArg(Arg arg) {
        this.arg = arg;
    }
    public String getDatapoints() {
        return datapoints;
    }
    public void setDatapoints(String datapoints) {
        this.datapoints = datapoints;
    }
    
    
}

input String for datapoints is like this

{"1694473620":0.0008232249347420499,"1694509620":0.0008405554897869077,"1694545620":0.0008438824476576583,"1694581620":0.0008171293069653031,"1694617620":0.0008596749524634415,"1694653620":0.0008368097665295172}

2

Answers


  1. I tried a lot but couldn’t figure out proper solution to remove backslash.

    I think you can remove the backslash in the datapoints field using the String.replaceAll() method:

    public class Response {
    
        private String id;
        private Arg arg;
        private String datapoints;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public Arg getArg() {
            return arg;
        }
    
        public void setArg(Arg arg) {
            this.arg = arg;
        }
    
        public String getDatapoints() {
            return datapoints;
        }
    
        public void setDatapoints(String datapoints) {
            this.datapoints = datapoints.replaceAll("\\", "");
        }
    }
    

    P.s.

    I hope all good. Let me know if it works for you, please.

    Login or Signup to reply.
  2. You should have the following structure. I used Jackson to handle deserialization/serialization, and Lombok to handle getters/setters/toString for the POJO fields.

    As others have mentioned, the type for datapoints should be a Map of String to Double.

    import java.util.Arrays;
    import java.util.Map;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.Data;
    
    public class ParseJson {
        private static final String SAMPLE_JSON_DATA = "[{"id":"346id71","arg":{"test1":"pass","task":"true","direction":"right"},"datapoints":{"1680012000":0.10124447461480172,"1680048000":8.398348387776654,"1680084000":8.338508371876594,"1680120000":8.357523374646366}},{"id":"346id72","arg":{"test1":"pass","task":"true","direction":"left"},"datapoints":{"1680012000":0.123233,"1680048000":3.567384,"1680084000":4.47653,"1680120000":0,"1680156000":0}}]";
    
        private static ObjectMapper mapper = new ObjectMapper();
    
        public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
            Response[] response = mapper.readValue(SAMPLE_JSON_DATA, Response[].class);
            System.out.println("nOUTPUT :- Object::toString");
            System.out.println(Arrays.toString(response));
            System.out.println("nOUTPUT :- ObjectMapper::writeValueAsString");
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(response));
        }
    
        @Data
        private static class Arg {
            String test1, task, direction;
        }
    
        @Data
        private static class Response {
            private String id;
            private Arg arg;
            private Map<String, Double> datapoints;
        }
    }
    

    Output

    Here is the .toString() call:

    [
      ParseJson.Response(
        id=346id71,
        arg=ParseJson.Arg(test1=pass, task=true, direction=right),
        datapoints={
          1680012000=0.10124447461480172,
          1680048000=8.398348387776654,
          1680084000=8.338508371876594,
          1680120000=8.357523374646366}
      ),
      ParseJson.Response(
        id=346id72,
        arg=ParseJson.Arg(test1=pass, task=true, direction=left),
        datapoints={
          1680012000=0.123233,
          1680048000=3.567384,
          1680084000=4.47653,
          1680120000=0.0,
          1680156000=0.0}
      )
    ]
    

    And the .writerWithDefaultPrettyPrinter().writeValueAsString() call:

    [ {
      "id" : "346id71",
      "arg" : {
        "test1" : "pass",
        "task" : "true",
        "direction" : "right"
      },
      "datapoints" : {
        "1680012000" : 0.10124447461480172,
        "1680048000" : 8.398348387776654,
        "1680084000" : 8.338508371876594,
        "1680120000" : 8.357523374646366
      }
    }, {
      "id" : "346id72",
      "arg" : {
        "test1" : "pass",
        "task" : "true",
        "direction" : "left"
      },
      "datapoints" : {
        "1680012000" : 0.123233,
        "1680048000" : 3.567384,
        "1680084000" : 4.47653,
        "1680120000" : 0.0,
        "1680156000" : 0.0
      }
    } ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search