2017-09-01 36 views
1

here不走了近遠遠不夠在解釋如何產生更復雜的結構的例子...使用JSON API序列化,以創建更復雜的JSON

如果我想要的東西,如落得:

{ 
    "data": { 
    "type": "mobile_screens", 
    "id": "1", 
    "attributes": { 
     "title": "Watch" 
    }, 
    "relationships": { 
     "mobile_screen_components": { 
     "data": [ 
      { 
      "id": "1_1", 
      "type": "mobile_screen_components" 
      }, 
      { 
      "id": "1_2", 
      "type": "mobile_screen_components" 
      }, 
      ... 
     ] 
     } 
    } 
    }, 
    "included": [ 
    { 
     "id": "1_1", 
     "type": "mobile_screen_components", 
     "attributes": { 
     "title": "Featured Playlist", 
     "display_type": "shelf" 
     }, 
     "relationships": { 
     "playlist": { 
      "data": { 
      "id": "938973798001", 
      "type": "playlists" 
      } 
     } 
     } 
    }, 
    { 
     "id": "938973798001", 
     "type": "playlists", 
     "relationships": { 
     "videos": { 
      "data": [ 
      { 
       "id": "5536725488001", 
       "type": "videos" 
      }, 
      { 
       "id": "5535943875001", 
       "type": "videos" 
      } 
      ] 
     } 
     } 
    }, 
    { 
     "id": "5536725488001", 
     "type": "videos", 
     "attributes": { 
     "duration": 78321, 
     "live_stream": false, 
     "thumbnail": { 
      "width": 1280, 
      "url": 
      "http://xxx.jpg?pubId=694940094001", 
      "height": 720 
     }, 
     "last_published_date": "2017-08-09T18:26:04.899Z", 
     "streams": [ 
      { 
      "url": 
       "http://xxx.m3u8", 
      "mime_type": "MP4" 
      } 
     ], 
     "last_modified_date": "2017-08-09T18:26:27.621Z", 
     "description": "xxx", 
     "fn__media_tags": [ 
      "weather", 
      "personality" 
     ], 
     "created_date": "2017-08-09T18:23:16.830Z", 
     "title": "NOAA predicts most active hurricane season since 2010", 
     "fn__tve_authentication_required": false 
     } 
    }, 
    ..., 
    ] 
} 

什麼是我可以設置的最簡單的數據結構和序列化程序?

我得到的東西等之後難倒:

const mobile_screen_components = responses.map((currentValue, index) => { 
    id[`id_${index}`]; 
}); 
const dataSet = { 
    id: 1, 
    title: 'Watch', 
    mobile_screen_components, 
}; 
const ScreenSerializer = new JSONAPISerializer('mobile_screens', { 
    attributes: ['title', 'mobile_screen_components'], 
    mobile_screen_components: { 
    ref: 'id', 
    } 
}); 

其中僅給了我:

{ 
    "data": { 
    "type": "mobile_screens", 
    "id": "1", 
    "attributes": { "title": "Watch" }, 
    "relationships": { 
     "mobile-screen-components": { 
     "data": [ 
      { "type": "mobile_screen_components", "id": "1_0" }, 
      { "type": "mobile_screen_components", "id": "1_1" }, 
      { "type": "mobile_screen_components", "id": "1_2" }, 
      { "type": "mobile_screen_components", "id": "1_3" }, 
      { "type": "mobile_screen_components", "id": "1_4" }, 
      { "type": "mobile_screen_components", "id": "1_5" } 
     ] 
     } 
    } 
    } 
} 

我不知道如何獲得「包括」兄弟姐妹「的數據。」等

+0

我認爲你應該儘可能地解決你的問題。看到https://stackoverflow.com/help/how-to-ask – Kamran

+0

@ kamran刪除了一些更多的最初的JSON結構,但這是儘可能少的表示,我可以做... –

+0

雖然它不是立即清楚,這個問題是基於使用https://github.com/SeyZ/jsonapi-serializer與建立使用http://jsonapi.org/規範的API的意圖。 –

回答

1

所以,問題是:

什麼是最簡單的數據結構和串行我可以設置?

下面是在問題使用jsonapi-serializer可以轉換到JSON類似於JSON最簡單的對象:

let dataSet = { 
    id: '1', 
    title: 'Watch', 
    mobile_screen_components: [ 
    { 
     id: '1_1', 
     title: 'Featured Playlists', 
     display_type: 'shelf', 
     playlists: { 
     id: 938973798001, 
     videos: [ 
      { 
      id: 5536725488001, 
      duration: 78321, 
      live_stream: false 
      }, 
      { 
      id: 5535943875001, 
      duration: 52621, 
      live_stream: true 
      } 
     ] 
     } 
    } 
    ] 
}; 

序列化此目的是JSON API,我用下面的代碼:

let json = new JSONAPISerializer('mobile_screen', { 
    attributes: ['id', 'title', 'mobile_screen_components'], 
    mobile_screen_components: { 
    ref: 'id', 
    attributes: ['id', 'title', 'display_type', 'playlists'], 
    playlists: { 
     ref: 'id', 
     attributes: ['id', 'videos'], 
     videos: { 
     ref: 'id', 
     attributes: ['id', 'duration', 'live_stream'] 
     } 
    } 
    } 
}).serialize(dataSet); 

console.log(JSON.stringify(json, null, 2)); 
  1. 構造函數JSONAPISerializer的第一個參數是資源類型。
  2. 第二個參數是序列化選項。
  3. 選項的每個級別等於序列化對象中嵌套對象的級別。
  4. ref - 如果存在,則視爲關係。
  5. attributes - 要顯示的屬性數組。
1

介紹

首先我們必須瞭解JSON APIdocument data structure

[0.1]指的頂層(對象根鍵):

甲文件必須至少包含以下頂級 成員之一:

data: the document’s 「primary data」 
errors: an array of error objects 
meta: a meta object that contains non-standard meta-information. 

文檔MAY包含任何這些頂層成員組成:

jsonapi: an object describing the server’s implementation 
links: a links object related to the primary data. 
included: an array of resource objects that are related to the primary data and/or each other (「included resources」). 

[0.2]

該文件的「主數據」是表示的資源 請求所針對的資源集合。

主數據MUST是以下之一:

  1. 單個資源標識符對象,或 空,爲靶向單資源的請求
  2. 資源標識符 對象的數組,一個空數組( []),請求。該目標 集合

以下主數據是一個單一的資源對象:

{ 
    "data": { 
    "type": "articles", 
    "id": "1", 
    "attributes": { 
     // ... this article's attributes 
    }, 
    "relationships": { 
     // ... this article's relationships 
    } 
    } 
} 

在(jsonapi-serializer)文檔:Available serialization option (opts argument)

所以爲了一個DD的included頂層構件)我進行以下測試:

var JsonApiSerializer = require('jsonapi-serializer').Serializer; 
const DATASET = { 
    id:23,title:'Lifestyle',slug:'lifestyle', 
    subcategories: [ 
    {description:'Practices for becoming 31337.',id:1337,title:'Elite'}, 
    {description:'Practices for health.',id:69,title:'Vitality'} 
    ] 
} 
const TEMPLATE = { 
    topLevelLinks:{self:'http://example.com'}, 
    dataLinks:{self:function(collection){return 'http://example.com/'+collection.id}}, 
    attributes:['title','slug','subcategories'], 
    subcategories:{ref:'id',attributes:['id','title','description']} 
} 
let SERIALIZER = new JsonApiSerializer('pratices', DATASET, TEMPLATE) 
console.log(SERIALIZER) 

隨着下面的輸出:

{ links: { self: 'http://example.com' }, 
    included: 
    [ { type: 'subcategories', id: '1337', attributes: [Object] }, 
    { type: 'subcategories', id: '69', attributes: [Object] } ], 
    data: 
    { type: 'pratices', 
    id: '23', 
    links: { self: 'http://example.com/23' }, 
    attributes: { title: 'Lifestyle', slug: 'lifestyle' }, 
    relationships: { subcategories: [Object] } } } 

正如你可以觀察到,被正確地填充的included


注意:如果你需要更多的幫助,你的dataSet,與原始數據編輯你的問題。

+0

那麼長的一點JSON(我的第一個引用的「代碼」)代表了數據。或者至少需要如何輸出。 –

+0

@ two7s_clash:你需要創建一個適合你的數據結構的'TEMPLATE',我們不能爲你做這件事,因爲你沒有顯示原始數據,只是一個輸出。解析器的模板是根據您的原始數據構建的,以獲得您真正想要的輸出 – EMX

+0

輸出數據與輸入數據相同,前者只是以特定方式序列化。正如你可以從上面的答案中看到的,我的問題是如何格式化數據作爲輸入,以給出我想要的輸出形式。完全可行。 –