我需要编写一个验证器来检查使用Gson从JSON解析的房间,即对于每对房间A和B,如果您可以从A到B,然后你可以从B到A检查一组是否对称的有效方法
下面是该JSON格式: https://jsfiddle.net/tgtbqzky/
{
"initialRoom": "MatthewsStreet",
"rooms": [
{
"name": "MatthewsStreet",
"description": "You are on Matthews, outside the Siebel Center",
"directions": [
{
"direction": "East",
"room": "SiebelEntry"
}
]
},
{
"name": "SiebelEntry",
"description": "You are in the west entry of Siebel Center. You can see the elevator, the ACM office, and hallways to the north and east.",
"directions": [
{
"direction": "West",
"room": "MatthewsStreet"
},
{
"direction": "Northeast",
"room": "AcmOffice"
},
{
"direction": "North",
"room": "SiebelNorthHallway"
},
{
"direction": "East",
"room": "SiebelEastHallway"
}
]
},
{
"name": "AcmOffice",
"description": "You are in the ACM office. There are lots of friendly ACM people.",
"directions": [
{
"direction": "South",
"room": "SiebelEntry"
}
]
},
{
"name": "SiebelNorthHallway",
"description": "You are in the north hallway. You can see Siebel 1112 and the door toward NCSA.",
"directions": [
{
"direction": "South",
"room": "SiebelEntry"
},
{
"direction": "NorthEast",
"room": "Siebel1112"
}
]
},
{
"name": "Siebel1112",
"description": "You are in Siebel 1112. There is space for two code reviews in this room.",
"directions": [
{
"direction": "West",
"room": "SiebelNorthHallway"
}
]
},
{
"name": "SiebelEastHallway",
"description": "You are in the east hallway. You can see Einstein Bros' Bagels and a stairway.",
"directions": [
{
"direction": "West",
"room": "SiebelEntry"
},
{
"direction": "South",
"room": "Siebel1314"
},
{
"direction": "Down",
"room": "SiebelBasement"
}
]
},
{
"name": "Siebel1314",
"description": "You are in Siebel 1314. There are happy CS 126 students doing a code review.",
"directions": [
{
"direction": "North",
"room": "SiebelEastHallway"
}
]
},
{
"name": "SiebelBasement",
"description": "You are in the basement of Siebel. You see tables with students working and door to computer labs.",
"directions": [
{
"direction": "Up",
"room": "SiebelEastHallway"
}
]
}
]
}
我在想,如果要走的路将是两个嵌套的循环,其中,环外将循环通过所有的房间,内部会循环通过每个循环内可能的每个方向,然后我将每一对我加入到一个Arr ayList。
如果我遇到了一些已经存在的东西,我会从ArrayList中移除它,并且如果在我的for循环结尾,ArrayList仍然包含一个元素,这意味着它的对应对不存在,并且因此JSON无效。如果ArrayList的大小为零,那么数据是有效的。
有没有人有更有效的方法来解决这个问题?我觉得自从它本质上证明给定集合是否是对称的,必须有一个更优化的方法。
你确实有一个代码将json反序列化为一个类的权利? – UmNyobe
是的,我有,我拥有所有的课程和功能。 – franklinsing