使用Gson序列化嵌套结构

时间:2017-11-22 09:22:05

标签: java serialization gson

我正在解决以下问题。 我们提供的服务如下:

"err": {
    "IoTMessageServiceImpl": {
        "Connection lost": 1,
        "Could not reconnect client": 9
    },
    "Messaging": {
        "Message not sent: Client is not connected": 1
    }
}

基本上,这是一个 err 对象,其中包含嵌套映射列表。 IoTMessageServiceImpl和Messaging元素的关键和价值是可变的。所以我知道我应该创建两个名为IoTMessageServiceImpl和Messaging的对象,它们都有一个映射列表。但是,我无法以正确的方式在Java中获得此结构。我最接近的人给了我以下输出:

"err": {
    "IoTMessageServiceImpl": {
        "IoTMessageServiceImpl": [null,null]
    },
    "Messaging": {
        "message": [null]
    }
}

有没有人知道如何构建我的类以使用Gson获得正确的输出? 提前谢谢!

此致

3 个答案:

答案 0 :(得分:1)

{     "连接丢失":1,     "无法重新连接客户端":9 }

{     "未发送消息:客户端未连接":1 }

不是地图列表。 它们是地图。

答案 1 :(得分:0)

使用以下内容修复它。 谢谢你以正确的方式引导我@Uttam

private HashMap<String, Integer> IoTMessageServiceImpl;
private HashMap<String, Integer> Messaging;

答案 2 :(得分:-1)

试试这个

class ErrorDTO {
    private IoTMessageServiceImplDTO IoTMessageServiceImpl;
    private MessagingDTO messaging;
}


class IoTMessageServiceImplDTO {
    ArrayList<String> IoTMessageServiceImpl;
}

class MessagingDTO {
    ArrayList<String> IoTMessageServiceImpl;
}
相关问题