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
I think you can remove the backslash in the datapoints field using the
String.replaceAll()
method:P.s.
I hope all good. Let me know if it works for you, please.
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 aMap
ofString
toDouble
.Output
Here is the
.toString()
call:And the
.writerWithDefaultPrettyPrinter().writeValueAsString()
call: