【发布时间】:2018-04-09 01:58:21
【问题描述】:
我的服务提供了以下 JSON:
[
{
"name":"Voter1",
"id":1,
"votingCard":{
"verificationCodes":[
"3I08jA",
"3x0eyE",
"2_i69I"
],
"votingCode":"7zCOelDnjfBm7TtFydc4QodgonG",
"finalizationCode":"jyYu",
"confirmationCode":"4ACfcpBVH45iAXqg7hJ0tbEe_tV"
}
},
.....
{
"id":5,
"name":"Voter5",
"votingCard":{
"verificationCodes":[
"2c9I9a",
"3bEeEa",
"1gPKx2"
],
"confirmationCode":"4Z7wNG35VR2UMO6-W-0aZVEhbLM",
"votingCode":"6YQ2x-c8LXJZF05gh3zTajU79ct",
"finalizationCode":"S0CY"
}
}
]
并且希望将其标准化,因此需要一个 VotingCard 列表和一个具有“votingCard”属性的选民列表,该属性通过 id 引用 VotingCard。
import { normalize, schema } from 'normalizr';
const votingCard = new schema.Entity('votingCard');
const voter = new schema.Entity('voter', {
votingCard: votingCard,
});
const votersSchema = new schema.Array(voter);
const mutations = {
SOCKET_SYNCVOTERS: (state, data) => {
var input = JSON.parse(data);
const normalizedData = normalize(input, votersSchema);
console.log(normalizedData);
},
};
但是,我没有得到我想要的:
为什么会有“未定义”?
【问题讨论】:
-
你没有在上面的源代码中向我们展示相同的 json 节点
-
嗯,你是什么意思?图片显示了
console.log(normalizedData);的输出,而第一个 JSON 列表来自console.log(data);。我错过了什么吗? -
在您发布的图片中,出现错误的是“Voter5”,而不是“name”属性,它似乎有一个“voter”属性。在您问题的开头,向我们展示您的 json 中如何定义 Voter5。可能是那个特定节点的问题。
-
啊,我明白你的意思了,我更新了帖子。
标签: javascript json normalizr