具有复合键的地图的JSON表示 [英] JSON Representation of Map with Complex Key
本文介绍了具有复合键的地图的JSON表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将以下(java)数据结构序列化为JSON:
I want to serialize to JSON the following (java) data structure:
class Machine {
String name;
Map<PartDescriptor, Part> parts;
}
class PartDescriptor {
String group;
String id;
hashCode()
equals()
}
class Part {
String group;
String id;
String description;
String compat;
...
...
}
什么会是一个机器的JSON表示
?
另外(可选),指向我一个JSON到Java序列化器/反序列化器,它将支持你的表示
Also (optional), point me to a JSON to Java serializer/deserializer that will support your representation
推荐答案
我会这样做:
{
"name": "machine name",
"parts": [
{ "group": "part group", "id": "part id", "description": "...", ... },
{ "group": "part group", "id": "part id", "description": "...", ... },
// ...
]
}
如果每个零件的id是唯一的,那么parts属性可以是一个对象而不是一个数组,每个零件的id服务作为关键。
If the "id" for each Part is unique, then the "parts" property can be an object instead of an array, with the "id" of each part serving as the key.
{
"name": "machine name",
"parts": {
"1st part id": { "group": "part group", "description": "...", ... },
"2nd part id": { "group": "part group", "description": "...", ... },
// ...
}
}
这篇关于具有复合键的地图的JSON表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文