1

所以,我从服务器得到一个 JSON 响应,看起来像:

{
  data: [
     { id:  1, type: 'person', emails: [ { id: 1 }, { id: 3 } ], phones: []  },
     { id:  2, type: 'person', emails: [ { id: 2 } ], phones: [ { id: 2 } ]  },
     { id:  3, type: 'person', emails: [ { id: 4 } ], phones: [ { id: 3 }, { id: 3 }]  }
  ],
  included: [
     { id: 1, type: 'emails', ... },
     { id: 2, type: 'emails', ... },
     { id: 3, type: 'emails', ... },
     { id: 4, type: 'emails', ... },
     { id: 1, type: 'phones', ... },
     { id: 2, type: 'phones', ... },
     { id: 3, type: 'phones', ... }
  ]
}

data属性是所有具有相同结构的接触对象的数组。每个联系人对象都有一系列相关的电子邮件和电话。

included属性是所有类型的相关对象的数组,这意味着它们可以共享和标识甚至具有不同的对象结构。

我希望尝试使响应变平以更易于使用并类似于:

{
    entities: {
        contacts: [ ... ],
        emails: [ ... ],
        phones: [ ... ]
    },
    result: [ 1, 2, 3 ] 
}

我已经设法使用以下方法规范化联系人数据:

const contactSchema = new schema.Entity('contacts');
const contactListSchema = [ contactSchema ];
const normalizedData= normalize(response, contactListSchema);

但这显然不包括entities.

我实际上不知道这个库是否能够实现我想要实现的目标,但我们将不胜感激。

虽然不是基于上述数据,但 API 是基于jsonapi.org模式,因此主页上的示例与结构完全匹配。

4

1 回答 1

1

我实际上找到了一个专门设计用于基于原始 normalizr 执行此操作的库:

https://github.com/stevenpetryk/jsonapi-normalizer

希望这可以帮助将来的人!

于 2017-01-20T14:56:00.030 回答