2016-02-26 90 views
0

在JSON API中,我需要表示主数据对象与其自身之间具有多对多关系的主数据集合。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" 
    } 
}] 

}

回答

1

嗯,首先,你粘贴的JSON是不是有效的JSON。您可以在http://jsonlint.com验证任何JSON。 我想你粘贴你的JSON跳过附件“}”字符。根据上面提到的页面,如果我把它放到你的JSON的末尾,这似乎是正确的。

如果您想根据JSON-API规范验证您的JSON,您可以在http://jsonapi.org/schema上找到规范架构,并将其粘贴到此页上:http://jsonschemalint.com/以及您要验证的JSON。

修复它加入“}”的结束后,jsonschemalint告诉我,这是一个兼容JSON-API对象;-)

+0

哦!非常感谢!这很有帮助 – inforeqd

+0

很高兴帮助! p.s .:接受我的答案,如果它有助于解决您的问题;) – Pavol

相关问题