I am trying to get this output:
{
"columnNames":[
"fruit"
],
"values":[
[
"Apple is green"
]
],
"time":"2017-12-11T00:00:00Z"
}
But I am ending up with the following...
{
"columnNames":[
"fruit"
],
"values":[
[
{
"stringValue":"Apple is green",
"intValue":0,
"booleanValue":false
}
]
],
"time":null
}
Here is my code (so far) and I would appreciate if you could tell me where I am going wrong?
public static void main(String[] args) {
List<String> columnNames = new ArrayList<>();
columnNames.add("fruit");
List<List<ResponseValues>> responseValues = new ArrayList<>();
List<ResponseValues> values = Collections.singletonList(new ResponseValues("Apple is green"));
responseValues.add(values);
String expectedResult = createAJson(columnNames, responseValues);
System.out.println(expectedResult);
}
public static String createAJson(List<String> columnNames, List<List<ResponseValues>> values) {
String jsonBlock = "";
repBuilder repBuilder = new repBuilder();
ObjectMapper mapper = new ObjectMapper();
repBuilder.setColumnNames(columnNames);
repBuilder.setValues(values);
try {
jsonBlock = mapper.writeValueAsString(repBuilder);
}
catch (JsonProcessingException e) {
e.printStackTrace();
}
return jsonBlock;
}
And some boiler plate code to create the response value section:
@JsonPropertyOrder({
"columnNames",
"values",
"time"
})
public class RepBuilder {
public List<String> columnNames;
public List<List<ResponseValues>> values;
private String time;
@JsonProperty("columnNames")
public List<String> getColumnNames() {
return columnNames;
}
@JsonProperty("columnNames")
public void setColumnNames(List<String> columnNames) {
this.columnNames = columnNames;
}
@JsonProperty("values")
public List<List<ResponseValues>> getValues() {
return values;
}
@JsonProperty("values")
public void setValues(List<List<ResponseValues>> values) {
this.values = values;
}
@JsonProperty("time")
public String gettime() {
return time;
}
@JsonProperty("time")
public void settime(String time) {
this.time = time;
}
}
And response values: This was done this way so I can set STRING or INTEGER or BOOLEAN and add to list?
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseValues {
private String stringValue;
private int intValue;
private boolean booleanValue;
public ResponseValues(String stringValue) {
this.stringValue = stringValue;
}
public ResponseValues(int intValue) {
this.intValue = intValue;
}
public ResponseValues(boolean booleanValue) {
this.booleanValue = booleanValue;
}
public String getStringValue() {
return stringValue;
}
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public boolean getBooleanValue() {
return booleanValue;
}
public void setBooleanValue(boolean booleanValue) {
this.booleanValue = booleanValue;
}
}
Can anyone point out what I am missing? I also tried to use @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) but that doesn't work for me, any other ideas? I would appreciate if you use my code in context of your answer (as im still learning java)