将 Json 数组嵌套到对象

Posted

技术标签:

【中文标题】将 Json 数组嵌套到对象【英文标题】:Nested Json array to objects 【发布时间】:2021-07-19 18:04:16 【问题描述】:

我们正在开发一个集成工具,它在 JSON 下生成,它创建一个对象数组而不是对象,我们的最终应用程序不支持这种数组结构,我们无法在集成工具中更改结构,但是我们可以添加可以执行此操作的javascript。我添加了当前和预期的 JSON 有效负载。 dataReq 段内的括号需要删除提前谢谢!

当前 Json:


    "abc": [
        
            "abcId": "1",
            "dataReq": [
                
                    "url": "/api.css.com",
                    "body": 
                        "desc": "Sprint",
                        "na": "Customer Data",
                        "tzone": "America/Los_Angeles"
                    ,
                    "method": "PATCH",
                    "refData": "OP"
                ,
                [
                    
                        "url": "/api.css.com/v1",
                        "body": 
                            "day": "Monday",
                            "end": "18:00:00.000Z",
                            "start": "08:00:00.000Z",
                            "hours": "08",
                            "Type": "Normal"
                        ,
                        "method": "PATCH",
                        "refData": "timeslotN"
                    ,
                    
                        "url": "/api.css.com/v2",
                        "body": 
                            "DayOfWeek": "Tuesday",
                            "EndTime": "18:00:00.000Z",
                            "StartTime": "08:00:00.000Z",
                            "hours": "09",
                            "Type": "Normal"
                        ,
                        "method": "PATCH",
                        "refData": "timeslotN"
                    
                ],
                
                    "url": "/api.css.com/v3",
                    "body": 
                        "Name": "Spirit2",
                        "Type": "murchast",
                        "Parent": 
                            "accID": "2"
                        ,
                        "street": "CR Steet",
                        "city": "NYK",
                        "state": null,
                        "postal": "9020",
                        "state": null,
                        "country": "LZ",
                    ,
                    "method": "PATCH",
                    "refData": "Account"
                ,
                
                    "url": "/api.css.com/v4",
                    "body": 
                        "account": "12321",
                        "state": 
                            "statecode": "311"
                        
                    ,
                    "method": "PATCH",
                    "refData": "acst"
                
            ]
        
    ]

预期的 JSON:


    "abc": [
        
            "abcId": "1",
            "dataReq": [
                
                    "url": "/api.css.com",
                    "body": 
                        "desc": "Sprint",
                        "na": "Customer Data",
                        "tzone": "America/Los_Angeles"
                    ,
                    "method": "PATCH",
                    "refData": "OP"
                ,
                
                        "url": "/api.css.com/v1",
                        "body": 
                            "day": "Monday",
                            "end": "18:00:00.000Z",
                            "start": "08:00:00.000Z",
                            "hours": "08",
                            "Type": "Normal"
                        ,
                        "method": "PATCH",
                        "refData": "timeslotN"
                    ,
                    
                        "url": "/api.css.com/v2",
                        "body": 
                            "DayOfWeek": "Tuesday",
                            "EndTime": "18:00:00.000Z",
                            "StartTime": "08:00:00.000Z",
                            "hours": "09",
                            "Type": "Normal"
                        ,
                        "method": "PATCH",
                        "refData": "timeslotN"
                    ,
                
                    "url": "/api.css.com/v3",
                    "body": 
                        "Name": "Spirit2",
                        "Type": "murchast",
                        "Parent": 
                            "accID": "2"
                        ,
                        "street": "CR Steet",
                        "city": "NYK",
                        "state": null,
                        "postal": "9020",
                        "state": null,
                        "country": "LZ",
                    ,
                    "method": "PATCH",
                    "refData": "Account"
                ,
                
                    "url": "/api.css.com/v4",
                    "body": 
                        "account": "12321",
                        "state": 
                            "statecode": "311"
                        
                    ,
                    "method": "PATCH",
                    "refData": "acst"
                
            ]
        
    ]

【问题讨论】:

两者有什么区别?目前真的很难说 解决办法始终是从源头上解决问题。在客户端上对其进行黑客攻击会导致需要维护的混乱情况翻倍。它特别需要修复,因为列表中的第一项是一个对象,其余的都在一个数组中——源真的被破坏了,需要修复。 【参考方案1】:

尝试使用Array#flatMap

您可以简单地使用Array#flatMap 来展平dataReq 数组。

data.abc[0].dataReq = data.abc[0].dataReq.flatMap((d) => d);

const data=abc:[abcId:"1",dataReq:[url:"/api.css.com",body:desc:"Sprint",na:"Customer Data",tzone:"America/Los_Angeles",method:"PATCH",refData:"OP",[url:"/api.css.com/v1",body:day:"Monday",end:"18:00:00.000Z",start:"08:00:00.000Z",hours:"08",Type:"Normal",method:"PATCH",refData:"timeslotN",url:"/api.css.com/v2",body:DayOfWeek:"Tuesday",EndTime:"18:00:00.000Z",StartTime:"08:00:00.000Z",hours:"09",Type:"Normal",method:"PATCH",refData:"timeslotN"],url:"/api.css.com/v3",body:Name:"Spirit2",Type:"murchast",Parent:accID:"2",street:"CR Steet",city:"NYK",state:null,postal:"9020",state:null,country:"LZ",method:"PATCH",refData:"Account",url:"/api.css.com/v4",body:account:"12321",state:statecode:"311",method:"PATCH",refData:"acst"]];

data.abc[0].dataReq = data.abc[0].dataReq.flatMap((d) => d);

console.log(JSON.stringify(data))

【讨论】:

@evolutionxbox 请不要进行不必要的编辑 请您停止对不是标题的文本使用标题吗? @evolutionxbox 这是我的标题。 它的标题是什么?那么,请您在答案中添加一些解释,以便标题具有标题的含义吗? @evolutionxbox 这只是为了引起注意,以便任何人都可以快速理解答案的含义。这是个人喜好,我想没有人应该对此有任何问题。【参考方案2】:

这是使用object-scan 的通用解决方案。

此解决方案会将双嵌套数组向上拉一级。

// const objectScan = require('object-scan');

const data1 =  abc: [  abcId: '1', dataReq: [  url: '/api.css.com', body:  desc: 'Sprint', na: 'Customer Data', tzone: 'America/Los_Angeles' , method: 'PATCH', refData: 'OP' , [  url: '/api.css.com/v1', body:  day: 'Monday', end: '18:00:00.000Z', start: '08:00:00.000Z', hours: '08', Type: 'Normal' , method: 'PATCH', refData: 'timeslotN' ,  url: '/api.css.com/v2', body:  DayOfWeek: 'Tuesday', EndTime: '18:00:00.000Z', StartTime: '08:00:00.000Z', hours: '09', Type: 'Normal' , method: 'PATCH', refData: 'timeslotN'  ],  url: '/api.css.com/v3', body:  Name: 'Spirit2', Type: 'murchast', Parent:  accID: '2' , street: 'CR Steet', city: 'NYK', state: null, postal: '9020', state: null, country: 'LZ' , method: 'PATCH', refData: 'Account' ,  url: '/api.css.com/v4', body:  account: '12321', state:  statecode: '311'  , method: 'PATCH', refData: 'acst'  ]  ] ;

const modify = objectScan(['**.*[*][*]'], 
  rtn: 'count',
  filterFn: ( parent, gparent, property, gproperty ) => 
    gparent.splice(gproperty + 1, 0, parent[property]);
    parent.splice(property, 1);
    if (parent.length === 0) 
      gparent.splice(gproperty, 1);
    
  
);

console.log(modify(data1));
// => 2

console.log(data1);
// =>  abc: [  abcId: '1', dataReq: [  url: '/api.css.com', body:  desc: 'Sprint', na: 'Customer Data', tzone: 'America/Los_Angeles' , method: 'PATCH', refData: 'OP' ,  url: '/api.css.com/v1', body:  day: 'Monday', end: '18:00:00.000Z', start: '08:00:00.000Z', hours: '08', Type: 'Normal' , method: 'PATCH', refData: 'timeslotN' ,  url: '/api.css.com/v2', body:  DayOfWeek: 'Tuesday', EndTime: '18:00:00.000Z', StartTime: '08:00:00.000Z', hours: '09', Type: 'Normal' , method: 'PATCH', refData: 'timeslotN' ,  url: '/api.css.com/v3', body:  Name: 'Spirit2', Type: 'murchast', Parent:  accID: '2' , street: 'CR Steet', city: 'NYK', state: null, postal: '9020', country: 'LZ' , method: 'PATCH', refData: 'Account' ,  url: '/api.css.com/v4', body:  account: '12321', state:  statecode: '311'  , method: 'PATCH', refData: 'acst'  ]  ] 
.as-console-wrapper max-height: 100% !important; top: 0
<script src="https://bundle.run/object-scan@15.0.0"></script>

免责声明:我是object-scan的作者

这里有一个例子可以更清楚地说明发生了什么

// const objectScan = require('object-scan');

const data2 =  a:  b: [[1, [2, 3]],  c: 4 ] , d: [ e: 5 , 6, [7, 8]] ;

const modify = objectScan(['**.*[*][*]'], 
  rtn: 'count',
  filterFn: ( parent, gparent, property, gproperty ) => 
    gparent.splice(gproperty + 1, 0, parent[property]);
    parent.splice(property, 1);
    if (parent.length === 0) 
      gparent.splice(gproperty, 1);
    
  
);

console.log(modify(data2));
// => 4

console.log(data2);
// =>  a:  b: [ 1, [ 2, 3 ],  c: 4  ] , d: [  e: 5 , 6, 7, 8 ] 
.as-console-wrapper max-height: 100% !important; top: 0
<script src="https://bundle.run/object-scan@15.0.0"></script>

免责声明:我是object-scan的作者

【讨论】:

以上是关于将 Json 数组嵌套到对象的主要内容,如果未能解决你的问题,请参考以下文章

将嵌套的 JSON 对象展平并排序到 javascript 中的数组中

RestKit:将嵌套数组映射到对象

如何将嵌套的 JSON 对象转换为数组 Spring Boot?

Json.NET:将嵌套数组反序列化为强类型对象

如何在 Ios 中解析数组数据中的嵌套 Json 对象

将 JSON 反序列化为 C# 对象以在网格中将嵌套数组显示为字符串