我想用JSON Form Playground從JSON動態創建html表單,這將允許他們編輯已經存在的請求。我有請求保持在db的某個地方,我有映射請求類。我想將json請求轉換爲JSON Form請求格式。創建JSON JFORM請求
防爆 - 持久化的請求
{"message":"My message","author":{"name":"author name","gender":"male","magic":36}}
映射類
public class DummyRequest
{
@SerializedName("message")
private String message;
@SerializedName("author")
private Author author;
// constructors, getters and setters ommitted
public static class Author
{
@SerializedName("name")
private String name;
@SerializedName("gender")
private Gender gender;
@SerializedName("magic")
private Integer magic;
}
public static enum Gender
{
male, female, alien
}
}
我創建的上述請求,其被持久化爲如下:從上述
public static void main(String[] args)
{
DummyRequest dummyRequest = new DummyRequest();
dummyRequest.setMessage("My message");
DummyRequest.Author author = new DummyRequest.Author("author name", DummyRequest.Gender.male, 36);
dummyRequest.setAuthor(author);
String dummyRequestJson = new Gson().toJson(dummyRequest);
System.out.println(dummyRequestJson);
}
現在,我想創建以下格式的JSON:
{
"schema": {
"message": {
"type": "string",
"title": "Message",
"default": "My message"
},
"author": {
"type": "object",
"title": "Author",
"properties": {
"name": {
"type": "string",
"title": "Name"
},
"gender": {
"type": "string",
"title": "Gender",
"enum": [ "male", "female", "alien" ]
},
"magic": {
"type": "integer",
"title": "Magic number",
"default": 42
}
},
"default": {"name": "Author name", "gender": "alien", "magic": 36}
}
}
}
這似乎相當複雜和乏味,如果我接近蠻力的方式。有人可以指導我如何繼續。我不想在Java中創建任何新的請求類。
您是要優化這個還是打開使用GSON庫 –
我打算使用gson庫。 – OneMoreError