【问题标题】:POJO for this complex JSON这个复杂 JSON 的 POJO
【发布时间】:2014-06-24 22:17:59
【问题描述】:

我是 POJO 的新手,我正在努力创建对象来表示来自 Google API 的 JSON。 如何为这个 JSON 创建一个 pojo?

"notificationSettings": {
    "notifications": [
     {
      "type": "eventCreation",
      "method": "email"
     },
     {
      "type": "eventChange",
      "method": "email"
     },
     {
      "type": "eventCancellation",
      "method": "email"
     },
     {
      "type": "eventResponse",
      "method": "email"
     }
    ]
   }

我已经弄清楚了 JSON 的其余部分,但是 Gson.fromJSON() 一直返回 null。

编辑 1

这就是我所拥有的

private static class NotificationSettings{

        public NotificationSettings(){

        }

        public NotificationSettings(Map<String, List<Notification>> notifications) {
            super();
            this.setNotifications(notifications);
        }

        public Map<String, List<Notification>> getNotifications() {
            return notifications;
        }

        public void setNotifications(Map<String, List<Notification>> notifications) {
            this.notifications = notifications;
        }

        private Map<String, List<Notification>> notifications;


    }

    private static class Notification{

        public Notification(){

        }

        public Notification(String type, String method) {
            super();
            this.type = type;
            this.method = method;
        }

        private String type;
        private String method;
        /**
         * @return the type
         */
        public String getType() {
            return type;
        }
        /**
         * @param type the type to set
         */
        public void setType(String type) {
            this.type = type;
        }
        /**
         * @return the method
         */
        public String getMethod() {
            return method;
        }
        /**
         * @param method the method to set
         */
        public void setMethod(String method) {
            this.method = method;
        }


    }

编辑 2 另一个问题,是否需要带参数的构造函数?另外,是不是也需要toString()方法?

谢谢!

【问题讨论】:

    标签: java json gson google-calendar-api pojo


    【解决方案1】:

    POJO 是一个普通的旧 Java 对象,它是一个具有字段和 getter/setter 的类。我不认为使用 Java 的人是 POJO 的新手,除非它正在学习 Java。这就是为什么我不会给出确切的类结构,否则我会做你的工作。

    您只需要定义 JSON 字符串中元素的类型。让我们回顾一下字符串以及如何映射它:

    "notificationSettings": { //name of class: NotificationSettings
        "notifications": [ //array of Notification objects. It means you will need a Notification class as well
                           //and an array or List field called notification in NotificationSettings class
         { //this defines the structure for Notification class
          "type": "eventCreation", //String field called type
          "method": "email" //String field called method
         },
         {
          "type": "eventChange",
          "method": "email"
         },
         {
          "type": "eventCancellation",
          "method": "email"
         },
         {
          "type": "eventResponse",
          "method": "email"
         }
        ]
    }
    

    从您的实现看来,您应该将 private Map&lt;String, List&lt;Notification&gt;&gt; notifications; 字段替换为 private List&lt;Notification&gt; notifications; 并将 Notification 类设为其自己文件中的顶级类。

    【讨论】:

    • 我用我的实现编辑了这个问题。谢谢你没有做所有的工作:P
    • 编辑有意义。我的印象是 GSON 可以序列化/反序列化嵌套类,只要它们是静态的。这里不是这样吗?
    • @Clocker 似乎 Gson 可以做到(如 here 所示),但我倾向于将类分开以进行维护。
    • 像魅力一样工作。我现在对 POJO 有了更好的了解,感谢您的帮助和您的快速回复@LuigiMendoza
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多