我有一个具有以下结构的JSON文件。
[
{
"type":[
"blog",
"article",
"pressrelease"
],
"states":[
"scheduled",
"published"
],
"roles":[
"editor",
"admin"
],
"actions":[
"review",
"delete"
]
},
{
"type":[
"blog",
"article",
"pressrelease"
],
"states":[
"draft",
"review"
],
"roles":[
"editor",
"admin"
],
"actions":[
"submit",
"delete"
]
},
{
"type":[
"blog",
"article"
],
"states":[
"draft",
"review"
],
"roles":[
"author"
],
"actions":[
"submit"
]
},
{
"type":[
"pressrelease"
],
"states":[
"draft",
"review"
],
"roles":[
"pr",
"prm"
],
"actions":[
"submit"
]
},
{
"type":[
"pressrelease"
],
"states":[
"scheduled",
"published"
],
"roles":[
"prm"
],
"actions":[
"review",
"delete"
]
}
]
我需要编写一个方法,根据上面的json返回动作,例如。
getActions(type, state, role){
}
我计划使用GSON将json转换为Action类对象列表,然后使用key作为type.state.role构建一个静态映射,并通过使用少量for循环处理json来将值作为操作列表。 我的地图将有
"blog.scheduled.editor" : ["review", "delete"]
"blog.scheduled.admin" : ["review", "delete"]
"blog.published.editor" : ["review", "delete"]
.....
"pressrelease.draft.pr" : ["submit"]
因此,在getActionas方法中,我可以通过从输入值创建键来轻松返回操作。
我不确定这是否是正确的实现,或者是否有实用工具。
答案 0 :(得分:1)
你走在正确的轨道上,但我建议使用树而不是地图,因为这是这种数据的更自然的表现。
像这样构建你的树:
* [Root]
|___ blog
| |___ published
| | |___ editor
| | | |___ review
| | | |___ delete
| | |
| | |___ admin
| | | |___ review
| | | |___ delete
| | |
| |
| |___ scheduled
| | |___ admin
| | | |___ delete
| | | |___ review
| | |
| | |___ editor
| | | |___ review
| | | |___ delete
| | |
| |
|
|___ pressrelease
| |___ scheduled
| | |___ admin
| | | |___ delete
| | | |___ review
(I spend far too much time on that ASCII tree ...)
然后你可以从节点到节点走三个“向下”并检查孩子们。
通过这种方式,您还可以检查博客条目上可能存在哪些状态(如果您以不同方式重新订购或创建树),您可以检查管理员可以对文章执行哪些操作。