gpt4 book ai didi

java - 尝试在 Java 中创建 json 对象但获取嵌套数组?如何以更好的方式做到这一点?

转载 作者:行者123 更新时间:2023-12-02 01:43:57 26 4
gpt4 key购买 nike

我正在尝试获得此输出:

{
"columnNames":[
"fruit"
],
"values":[
[
"Apple is green"
]
],
"time":"2017-12-11T00:00:00Z"
}

但我最终得到以下结果......

{
"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;
}
}

谁能指出我缺少什么?我还尝试使用@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)但这对我不起作用,还有其他想法吗? 如果您在答案中使用我的代码(因为我仍在学习java),我将不胜感激

最佳答案

ResponseValues自定义序列化器,例如:

public class ResponseSerializer extends JsonSerializer<ResponseValues>{ 

@Override
public void serialize(ResponseValues responseValues, JsonGenerator gen,
SerializerProvider provider) throws IOException,
JsonProcessingException {

gen.writeString(responseValues.getStringValue());
}

}

并将其与注释一起使用:

@JsonSerialize(using = ResponseSerializer.class)
public class ResponseValues

更新

如果您需要按顺序检查它们,可以这样做:

@Override
public void serialize(ResponseValues responseValues, JsonGenerator gen,
SerializerProvider provider) throws IOException,
JsonProcessingException {

String stringValue = responseValues.getStringValue();
if (stringValue != null) {
gen.writeString(responseValues.getStringValue());
return; // if string value is valid, write it and return
}

int intValue = responseValues.getIntValue();
// let's suppose 0 is not a valid status
if (intValue != 0) {
gen.writeNumber(intValue);
return;
// also you can use gen.writeString(String.valueOf(intValue)); if you need a String
}

boolean booleanValue = responseValues.getBooleanValue();
gen.writeBoolean(booleanValue);

}

关于java - 尝试在 Java 中创建 json 对象但获取嵌套数组?如何以更好的方式做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53988320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com