一种建议的接口返回结构
比如建议如下
{
"state": {
"code": 0,
"msg": "success"
},
"data": {
"list": [ // 这里多一层 list 是有好处的
{
"id": "xx",
"prefix": "xx",
"name": "xx",
"type": "2",
...
},
...
]
}
}
为什么上面的返回,要多加一层 list ? 而不是这样返回
{
"state": {
"code": 0,
"msg": "success"
},
"data": [
{
"id": "xx",
"prefix": "xx",
"name": "xx",
"type": "2",
...
},
...
]
}
好处
很明显,如果接口需要增减参数时,第一种有 list 层级的,前后端处理都会很方便。
比如我加一个字段 other
:
{
"state": {
"code": 0,
"msg": "success"
},
"data": {
"list": [ // 这里多一层 list 是有好处的
{
"id": "xx",
"prefix": "xx",
"name": "xx",
"type": "2",
...
},
...
],
"other" : [
...
]
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: