2017-09-01 91 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

输出数据与输入数据相同,前者只是以特定方式序列化。正如你可以从上面的答案中看到的,我的问题是如何格式化数据作为输入,以给出我想要的输出形式。完全可行。 –