我必须为json字符串编写一个Java类。以下是我的json字符串, 其中具有' properties '属性,任何键值对都可以出现,并且 value 还是 json对象。 'string,可以具有不同的属性,对于'type'number,它可以具有不同的属性..etc。
{
"type": "object",
"required": [
"a"
],
"properties": {
"a": {
"type": "number",
"description": "this is column a"
},
"b": {
"type": "date",
"description": "this is column b"
},
"fullName": {
"type": "string",
"description": "",
"minLength": 1,
"maxLength": 10
}
},
"title": "this is table",
"name": "test_table"
}
下面是我尝试过并起作用的代码
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.db.gtb.cdo.model.datatype.DataTypeModel;
import lombok.Data;
@Data
public class TableModel {
private String type;
private List<String> required = new ArrayList<String>();
private Map<String, DataTypeModel> properties = new LinkedHashMap<>();
private String title;
}
public abstract class DataTypeModel {
DataType type;
String description;
public DataTypeModel(DataType type, String description) {
this.type = type;
this.description = description;
}
public DataType getType() {
return type;
}
public String getDescription() {
return description;
}
}
import lombok.Getter;
import lombok.Setter;
public class StringDataTypeModel extends DataTypeModel {
@Setter @Getter Integer minLength;
@Setter @Getter Integer maxLength;
public StringDataTypeModel(DataType type, String description, Integer minLength, Integer maxLength) {
super(type, description);
this.minLength = minLength;
this.maxLength = maxLength;
}
}
public class NumberDataTypeModel extends DataTypeModel {
public NumberDataTypeModel(DataType type, String description) {
super(type, description);
}
}
public class DateDataTypeModel extends DataTypeModel {
public DateDataTypeModel(DataType type, String description) {
super(type, description);
}
}
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class Test {
public static void main(String args[]) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.writerWithDefaultPrettyPrinter();
TableModel table = new TableModel();
Map<String, DataTypeModel> properties = new HashMap<>();
properties.put("code", new StringDataTypeModel(DataType.string, "this is string", 1, 15,"/s"));
properties.put("startDate", new DateDataTypeModel(DataType.date, "this is date", "pattern"));
table.setProperties(properties);
table.setType("object");
table.setRequired(Arrays.asList("code"));
table.setTitle("this is branch table for bank");
mapper.writer().withRootName("branch").writeValue(new File("Table.json"), table);
//Object to JSON in String
String jsonInString = mapper.writer().withRootName("branch").writeValueAsString(table);
System.out.println(jsonInString);
}
}
上面的类生成预期的json结构
答案 0 :(得分:1)