请注意,JSON的结构事先是未知的,即它完全是任意的。我们只知道它的格式是JSON。
例如,
以下是JSON的示例
{
"Port":
{
"@alias": "defaultHttp",
"Enabled": "true",
"Number": "10092",
"Protocol": "http",
"KeepAliveTimeout": "20000",
"ThreadPool":
{
"@enabled": "false",
"Max": "150",
"ThreadPriority": "5"
},
"ExtendedProperties":
{
"Property":
[
{
"@name": "connectionTimeout",
"$": "20000"
}
]
}
}
}
应该反序列化为类似于Map结构的形式,具有像下面这样的键(为简洁起见,不包括所有内容):
port[0].alias
port[0].enabled
port[0].extendedProperties.connectionTimeout
port[0].threadPool.max
我目前正在研究Jackson,所以我们有:
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {};
Map<String, String> o = objectMapper.readValue(jsonString, typeRef);
然而,生成的Map实例基本上是嵌套Map的Map:
{Port={@alias=diagnostics, Enabled=false, Type=DIAGNOSTIC, Number=10033, Protocol=JDWP, ExtendedProperties={Property={@name=suspend, $=n}}}}
虽然我需要一个使用“点符号”展开键的扁平映射表,就像上面的那样。
但我宁愿不自己实现它,尽管目前我没有看到其他方法...