Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建JSON JFORM请求_Java_Json_Gson - Fatal编程技术网

Java 创建JSON JFORM请求

Java 创建JSON JFORM请求,java,json,gson,Java,Json,Gson,我试图使用JSON动态创建html表单,这将允许他们编辑已经存在的请求。我将请求保存在db中的某个地方,并且有映射请求类。我想将json请求转换为json表单请求格式 前持久化请求 {"message":"My message","author":{"name":"author name","gender":"male","magic":36}} 映射类 public class DummyRequest { @SerializedName("message") private

我试图使用JSON动态创建html表单,这将允许他们编辑已经存在的请求。我将请求保存在db中的某个地方,并且有映射请求类。我想将json请求转换为json表单请求格式

前持久化请求

{"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序列化数据,如下所示

     @SerializedName("schema")
       private Schema schema;
您的模式对象如下

       @SerializedName("message")
       private Message message;
       @SerializedName("author")
       private Author author;
           -- and so on
使用以下代码从json获取对象

    Gson gson=new Gson();
    Model yourModel=gson.fromJson(<your json object as string>);

您是要求对此进行优化,还是愿意使用GSON库我愿意使用GSON库。您在哪里为每个字段设置“类型”、“标题”、“默认值”?这将是一个单独的模型。你只需设置所有的模型一次,然后就可以轻松地在整个应用程序中进行设置
    Gson gson=new Gson();
    String string=gson.toJson(yourObject,YourObject.class);