2016-02-26 59 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

相關問題