在JSON API中,我需要表示主数据的集合,其中主数据对象与自身有很多关系。
即。
account1 -> canTransferTo (relationship) -account3
account2 -> canTransferTo (relationship) -account1, account3
account3 -> canTransferTo (relationship) -account1
我将如何做到这一点,以便遵循JSO API规范。可以这样吗?
{
"data": [{
"type": "accounts",
"id": "1",
"attributes": {
"name": "account1"
},
"relationships": {
"canTransferTo": {
"links": {
"self": "http://example.com/accounts/1/relationships/canTransferTo",
"related": "http://example.com/accounts/1/canTransferTo"
},
"data": {
"type": "accounts",
"id": "3"
}
}
},
"links": {
"self": "http://example.com/accounts/1"
}
}, {
"type": "accounts",
"id": "2",
"attributes": {
"name": "account2"
},
"relationships": {
"canTransferTo": {
"links": {
"self": "http://example.com/accounts/2/relationships/canTransferTo",
"related": "http://example.com/accounts/2/canTransferTo"
},
"data": [{
"type": "accounts",
"id": "1"
}, {
"type": "accounts",
"id": "3"
}]
}
},
"links": {
"self": "http://example.com/accounts/2"
}
}, {
"type": "accounts",
"id": "3",
"attributes": {
"name": "account3"
},
"relationships": {
"canTransferTo": {
"links": {
"self": "http://example.com/accounts/3/relationships/canTransferTo",
"related": "http://example.com/accounts/3/canTransferTo"
},
"data": {
"type": "accounts",
"id": "1"
}
}
},
"links": {
"self": "http://example.com/accounts/3"
}
}]
}
答案 0 :(得分:1)
首先,您粘贴的JSON不是有效的JSON。您可以在http://jsonlint.com验证任何JSON。 我猜你贴了你的JSON,跳过了封闭的"}" 字符。根据上面提到的页面,如果我把它放到你的JSON的末尾,它似乎是正确的。
如果要根据JSON-API规范验证JSON,可以在http://jsonapi.org/schema上找到规范架构并将其粘贴到此页面上:http://jsonschemalint.com/以及要验证的JSON。
修好后加上"}"到最后,jsonschemalint告诉我它是一个符合JSON-API的对象; - )