C#/Javascript:如何将包含父子数组的JSON字符串转换为多个父数组



我想将父子数组拆分为多个父子数组。JSON string如下:

[{
"Father-Name" : "Jack",
"Mother-Name" : "Rose",
"Children": [{ "Name" : "Lmao" }, {"Name" : "Lol"}]
},
{
"Father-Name" : "Donald",
"Mother-Name" : "Melissa",
"Children": null
}]

以及我想要的输出:

[{
"Father-Name" : "Jack",
"Mother-Name" : "Rose",
"Children": { "Name" : "Lmao" }
},
{
"Father-Name" : "Jack",
"Mother-Name" : "Rose",
"Children": {"Name" : "Lol"}
},
{
"Father-Name" : "Donald",
"Mother-Name" : "Melissa",
"Children": null
}]

我做了一些搜索,但没有匹配的。

答案就在这个链接解决方案中。感谢@JLRishe

我只修改了一张空支票。

var data = [{
"Father" : "Jack",
"Mother" : "Rose",
"Children": [{ "Name" : "Lmao" }, {"Name" : "Lol"}]
},
{
"Father" : "Donald",
"Mother" : "Melissa",
"Children": null
}]
var result = data.map(d => d.Children!== null ? d.Children.map(c => ({"Father": d.Father,"Mother": d.Mother, Children: {...c}})) : d).flat();
console.log(result);

最新更新