【问题标题】:Deserialize Java array/list using gson使用 gson 反序列化 Java 数组/列表
【发布时间】:2020-10-13 13:03:18
【问题描述】:

我在 Java 中使用 gson 来反序列化来自 Azure EventGrid 的一些 JSON(它基本上只是常规的 JSON 消息)。我不断收到malmformed JSON 引发的异常,或者只是像unterminated object at line 1 column 13 path $[0].msg 这样的奇怪错误

这就是我试图反序列化的 JSON 的样子:

{
  "id": "3984520-582350923-52389532042802",
  "subject": "/v1-1/URL_PATH_HERE",
  "data": [
    {
    "msg": "randomMsgHere",
    "time": 1599922804852,
    "type": "",
    "alarm": true,
    "source": "",
    "category": "",
    "severity": "",
    "elementId": "",
    "networkId": "",
    "hardwareType": "",
    "failureObject": "",
    "reportingAgent": "",
    "sourceDisplayName": "",
    "failureObjectDisplayName": "",
    "managedObjectDisplayName": "",
    "extendedAttributes": {
        "eventId": 100,
        "customerAlias": "string",
        "latitude": 10.00,
        "longitude": 10.00,
        "elevation": 10.00
    }
}
  ],
  "eventType": "eventType",
  "dataVersion": "1",
  "metadataVersion": "1",
  "eventTime": "2020-10-13T01:41:32.765Z",
  "topic": "randomTopicHere"
}

我只是在其中放了一些随机数据(假设空字符串有数据),我正在调用一个方法来提取或获取() JSON 的 data [ ] 属性,所以我应该得到一个数组。我已经在 J​​ava 中定义了一个数据类,所以我可以轻松地对其进行反序列化,但我通常使用来自 Jackson 的 Kotlin 数据类和对象映射器,我认为这个数组存在问题。

将此 JSON 字符串转换为像这样定义的 EventState.class 的最简单方法是什么?

@Getter
    @Setter
public class EventState {
    private String msg;
    private long time;
    private String type;
    private boolean alarm;
    private String source;
    private String category;
    private String severity;
    private String elementId;
    private String networkId;
    private String hardwareType;
    private String failureObject;
    private String reportingAgent;
    private String sourceDisplayName;
    private String failureObjectDisplayName;
    private String managedObjectDisplayName;
    private ExtendedAttributes extendedAttributes;

    @Getter
    @Setter
    public static class ExtendedAttributes {
        private int eventId;
        private String customerAlias;
        private Double latitude;
        private Double longitude;
        private Double elevation;
    }

【问题讨论】:

    标签: java json serialization gson deserialization


    【解决方案1】:

    上面的 json 字符串很容易通过 gson 解析,只需在 Eventstate 周围添加一个容器。

    这是一个例子:

    public class Test {
    
    public static void main(String[] args) {
        String value = "{\n" +
                "  \"id\": \"3984520-582350923-52389532042802\",\n" +
                "  \"subject\": \"/v1-1/URL_PATH_HERE\",\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "    \"msg\": \"randomMsgHere\",\n" +
                "    \"time\": 1599922804852,\n" +
                "    \"type\": \"\",\n" +
                "    \"alarm\": true,\n" +
                "    \"source\": \"\",\n" +
                "    \"category\": \"\",\n" +
                "    \"severity\": \"\",\n" +
                "    \"elementId\": \"\",\n" +
                "    \"networkId\": \"\",\n" +
                "    \"hardwareType\": \"\",\n" +
                "    \"failureObject\": \"\",\n" +
                "    \"reportingAgent\": \"\",\n" +
                "    \"sourceDisplayName\": \"\",\n" +
                "    \"failureObjectDisplayName\": \"\",\n" +
                "    \"managedObjectDisplayName\": \"\",\n" +
                "    \"extendedAttributes\": {\n" +
                "        \"eventId\": 100,\n" +
                "        \"customerAlias\": \"string\",\n" +
                "        \"latitude\": 10.00,\n" +
                "        \"longitude\": 10.00,\n" +
                "        \"elevation\": 10.00\n" +
                "    }\n" +
                "}\n" +
                "  ],\n" +
                "  \"eventType\": \"eventType\",\n" +
                "  \"dataVersion\": \"1\",\n" +
                "  \"metadataVersion\": \"1\",\n" +
                "  \"eventTime\": \"2020-10-13T01:41:32.765Z\",\n" +
                "  \"topic\": \"randomTopicHere\"\n" +
                "}";
        Gson gson = new Gson();
        EventStateContainer eventStateContainer = gson.fromJson(value, EventStateContainer.class);
        System.out.println(eventStateContainer.getData());
    }
    
    public class EventStateContainer {
        private String id;
        private String subject;
        private ArrayList<EventState> data;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getSubject() {
            return subject;
        }
    
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public ArrayList<EventState> getData() {
            return data;
        }
    
        public void setData(ArrayList<EventState> data) {
            this.data = data;
        }
    }
    
    public class EventState {
        private String msg;
        private long time;
        private String type;
        private boolean alarm;
        private String source;
        private String category;
        private String severity;
        private String elementId;
        private String networkId;
        private String hardwareType;
        private String failureObject;
        private String reportingAgent;
        private String sourceDisplayName;
        private String failureObjectDisplayName;
        private String managedObjectDisplayName;
        private ExtendedAttributes extendedAttributes;
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public long getTime() {
            return time;
        }
    
        public void setTime(long time) {
            this.time = time;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public boolean isAlarm() {
            return alarm;
        }
    
        public void setAlarm(boolean alarm) {
            this.alarm = alarm;
        }
    
        public String getSource() {
            return source;
        }
    
        public void setSource(String source) {
            this.source = source;
        }
    
        public String getCategory() {
            return category;
        }
    
        public void setCategory(String category) {
            this.category = category;
        }
    
        public String getSeverity() {
            return severity;
        }
    
        public void setSeverity(String severity) {
            this.severity = severity;
        }
    
        public String getElementId() {
            return elementId;
        }
    
        public void setElementId(String elementId) {
            this.elementId = elementId;
        }
    
        public String getNetworkId() {
            return networkId;
        }
    
        public void setNetworkId(String networkId) {
            this.networkId = networkId;
        }
    
        public String getHardwareType() {
            return hardwareType;
        }
    
        public void setHardwareType(String hardwareType) {
            this.hardwareType = hardwareType;
        }
    
        public String getFailureObject() {
            return failureObject;
        }
    
        public void setFailureObject(String failureObject) {
            this.failureObject = failureObject;
        }
    
        public String getReportingAgent() {
            return reportingAgent;
        }
    
        public void setReportingAgent(String reportingAgent) {
            this.reportingAgent = reportingAgent;
        }
    
        public String getSourceDisplayName() {
            return sourceDisplayName;
        }
    
        public void setSourceDisplayName(String sourceDisplayName) {
            this.sourceDisplayName = sourceDisplayName;
        }
    
        public String getFailureObjectDisplayName() {
            return failureObjectDisplayName;
        }
    
        public void setFailureObjectDisplayName(String failureObjectDisplayName) {
            this.failureObjectDisplayName = failureObjectDisplayName;
        }
    
        public String getManagedObjectDisplayName() {
            return managedObjectDisplayName;
        }
    
        public void setManagedObjectDisplayName(String managedObjectDisplayName) {
            this.managedObjectDisplayName = managedObjectDisplayName;
        }
    
        public ExtendedAttributes getExtendedAttributes() {
            return extendedAttributes;
        }
    
        public void setExtendedAttributes(ExtendedAttributes extendedAttributes) {
            this.extendedAttributes = extendedAttributes;
        }
    
        @Override
        public String toString() {
            return "EventState{" +
                    "msg='" + msg + '\'' +
                    ", time=" + time +
                    ", type='" + type + '\'' +
                    ", alarm=" + alarm +
                    ", source='" + source + '\'' +
                    ", category='" + category + '\'' +
                    ", severity='" + severity + '\'' +
                    ", elementId='" + elementId + '\'' +
                    ", networkId='" + networkId + '\'' +
                    ", hardwareType='" + hardwareType + '\'' +
                    ", failureObject='" + failureObject + '\'' +
                    ", reportingAgent='" + reportingAgent + '\'' +
                    ", sourceDisplayName='" + sourceDisplayName + '\'' +
                    ", failureObjectDisplayName='" + failureObjectDisplayName + '\'' +
                    ", managedObjectDisplayName='" + managedObjectDisplayName + '\'' +
                    ", extendedAttributes=" + extendedAttributes +
                    '}';
        }
    }
    
    public static class ExtendedAttributes {
        private int eventId;
        private String customerAlias;
        private Double latitude;
        private Double longitude;
        private Double elevation;
    
        public int getEventId() {
            return eventId;
        }
    
        public void setEventId(int eventId) {
            this.eventId = eventId;
        }
    
        public String getCustomerAlias() {
            return customerAlias;
        }
    
        public void setCustomerAlias(String customerAlias) {
            this.customerAlias = customerAlias;
        }
    
        public Double getLatitude() {
            return latitude;
        }
    
        public void setLatitude(Double latitude) {
            this.latitude = latitude;
        }
    
        public Double getLongitude() {
            return longitude;
        }
    
        public void setLongitude(Double longitude) {
            this.longitude = longitude;
        }
    
        public Double getElevation() {
            return elevation;
        }
    
        public void setElevation(Double elevation) {
            this.elevation = elevation;
        }
    
        @Override
        public String toString() {
            return "ExtendedAttributes{" +
                    "eventId=" + eventId +
                    ", customerAlias='" + customerAlias + '\'' +
                    ", latitude=" + latitude +
                    ", longitude=" + longitude +
                    ", elevation=" + elevation +
                    '}';
        }
    }
    

    }

    最好的问候

    托本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 2011-03-28
      • 1970-01-01
      • 2023-04-04
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多