我需要編寫一個驗證器來檢查使用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