【问题标题】:Create JSON String using GSON使用 GSON 创建 JSON 字符串
【发布时间】:2023-03-16 01:39:01
【问题描述】:

我正在上课,

public class Student {
    public int id;
    public String name;
    public int age;    
}

现在我想创建新的学生,

//while create new student
Student stu = new Student();
stu.age = 25;
stu.name = "Guna";
System.out.println(new Gson().toJson(stu));

这给了我以下输出,

{"id":0,"name":"Guna","age":25} //Here I want string without id, So this is wrong

所以这里我想要类似字符串

{"name":"Guna","age":25}

如果我想编辑旧学生

//While edit old student
Student stu2 = new Student();
stu2.id = 1002;
stu2.age = 25;
stu2.name = "Guna";
System.out.println(new Gson().toJson(stu2));

现在输出是

{"id":1002,"name":"Guna","age":25} //Here I want the String with Id, So this is correct

如何创建一个带有字段 [在某些时候] 的 JSON 字符串,而没有字段 [在某些时候]。

任何帮助都将不胜感激。

谢谢。

【问题讨论】:

  • 声明int变量时,默认值为0。int不能为null。所以我建议你改用 String 或者忽略 id 值,如果它是 0。
  • @joao2fast4u 我已经编辑了我的代码朋友
  • @Gunaseelan 检查我的答案以获得更好的解决方案。创建 json 后无需删除密钥。

标签: java android json gson


【解决方案1】:

您可以使用 gson 探索 json 树。

试试这样的:

gson.toJsonTree(stu1).getAsJsonObject().remove("id");

你也可以添加一些属性:

gson.toJsonTree(stu2).getAsJsonObject().addProperty("id", "100");

【讨论】:

    【解决方案2】:

    你有两个选择。

    • 使用Java 的transient 关键字表示不应序列化字段。 Gson 会自动排除它。有条件地,这可能不适合您。

    • 对你想要的字段使用@Expose注解并初始化你的Gson构建器如下:

      Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

    因此,您需要使用@Expose 标记姓名和年龄字段,并且您需要有两个不同的 Gson 实例,默认一个包含所有字段,一个排除没有 @Expose 注释的字段。

    【讨论】:

      【解决方案3】:
      JsonObject jsObj =  (JsonObject) new Gson().toJsonTree(stu2);
      jsObj.remove("age"); // remove field 'age'
      jsObj.addProperty("key", "value"); // add field 'key'
      
      System.out.println(jsObj);
      

      您可以使用 JsonObject 进行操作

      【讨论】:

        【解决方案4】:

        您应该向Student 类引入额外的字段,以通知GSON 关于id 序列化策略。 然后,您应该实现将实现TypeAdapter 的自定义序列化程序。在您的 TypeAdapter 实现中,根据 id 序列化策略,您将序列化或不序列化它。然后你应该在 GSON 工厂注册你的TypeAdapter

        GsonBuilder gson = new GsonBuilder();
        gson.registerTypeAdapter(Student.class, new StudentTypeAdapter());
        

        希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          更好的方法是使用@expose 注释

          public class Student {
              public int id;
              @Expose
              public String name;
              @Expose
              public int age;
          }
          

          并使用以下方法从您的对象中获取 Json 字符串

          private String getJsonString(Student student) {
              // Before converting to GSON check value of id
              Gson gson = null;
              if (student.id == 0) {
                  gson = new GsonBuilder()
                  .excludeFieldsWithoutExposeAnnotation()
                  .create();
              } else {
                  gson = new Gson();
              }
              return gson.toJson(student);
          }
          

          如果设置为 0,它将忽略 id 列,或者它会返回带有 id 字段的 json 字符串。

          【讨论】:

          • 是的,我认为这是更好的答案。谢谢兄弟
          • 如果我有一个对象作为类的属性之一,并且我在该对象中实现了 Gson?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-25
          相关资源
          最近更新 更多