2017-01-26 1 views
1

Dies ist das erste Mal, dass wir JSON API in unseren Projekten verwenden und nach Spezifikation auf ihrer Web, das ist, was eine reguläre JSON API-Antwort wieSollte das JSON API-Attributelement verschachtelte Objekte enthalten?

HTTP/1.1 200 OK 
Content-Type: application/vnd.api+json 

{ 
    "data": [{ 
    "type": "articles", 
    "id": "1", 
    "attributes": { 
     "title": "JSON API paints my bikeshed!", 
     "body": "The shortest article. Ever.", 
     "created": "2015-05-22T14:56:29.000Z", 
     "updated": "2015-05-22T14:56:28.000Z" 
    }, 
    "relationships": { 
     "author": { 
     "data": {"id": "42", "type": "people"} 
     } 
    } 
    }], 
    "included": [ 
    { 
     "type": "people", 
     "id": "42", 
     "attributes": { 
     "name": "John", 
     "age": 80, 
     "gender": "male" 
     } 
    } 
    ] 
} 

aussehen sollte uns nicht sicher sind, sollen Attribute in Daten immer flach oder Attribute auch verschachtelte Objekte enthalten könnte wie Standort zum Beispiel

"data": [{ 
     "type": "articles", 
     "id": "1", 
     "attributes": { 
      "title": "JSON API paints my bikeshed!", 
      "body": "The shortest article. Ever.", 
      "created": "2015-05-22T14:56:29.000Z", 
      "updated": "2015-05-22T14:56:28.000Z", 
      "location": 
      { 
      "lat": "0.00", 
      "long": "0.00"} 
     }, 

Antwort

1

Ja, werfen Sie einen Blick auf: http://jsonapi.org/format/#document-resource-object-attributes

Complex data structures involving JSON objects and arrays are allowed as attribute values. However, any object that constitutes or is contained in an attribute MUST NOT contain a relationships or links member, as those members are reserved by this specification for future use.

0

nach dem Decodieren Ihre JSON Ergebnis ist -

Array 
(
    [data] => Array 
     (
      [0] => Array 
       (
        [type] => articles 
        [id] => 1 
        [attributes] => Array 
         (
          [title] => JSON API paints my bikeshed! 
          [body] => The shortest article. Ever. 
          [created] => 2015-05-22T14:56:29.000Z 
          [updated] => 2015-05-22T14:56:28.000Z 
          [location] => Array 
           (
            [lat] => 0.00 
            [long] => 0.00 
           ) 

         ) 

        [relationships] => Array 
         (
          [author] => Array 
           (
            [data] => Array 
             (
              [id] => 42 
              [type] => people 
             ) 

           ) 

         ) 

       ) 

     ) 

    [included] => Array 
     (
      [0] => Array 
       (
        [type] => people 
        [id] => 42 
        [attributes] => Array 
         (
          [name] => John 
          [age] => 80 
          [gender] => male 
         ) 

       ) 

     ) 

) 

Und hier enthält Ort ein Array, also wird dies ein verschachteltes Objekt sein.

+0

meine Frage ist, tut json api innerhalb verschachtelten Objekten empfiehlt Attribute Element – mko

+0

Ja! json api empfiehlt verschachtelte Objekte. –

+0

Woher weißt du das? – mko

Verwandte Themen