JSON 数据拆分

这是在数据提交时遇到的问题。


我准备的数据结构是这样的:


    {

        "path": "test",

        "clients": [

            {

                "client": "1.2.2.2;1.1.1.1",

                "Access_Type": 2,

                "name": "test_01"

            },

            {

                "client": "1.2.2.4;1.1.1.4",

                "Access_Type": 1,

                "name": "test_02"

            },

            {

                "client": "1.3.3.3",

                "Access_Type": 1,

                "name": "test_03"

            }

        ]

    }

然而我在提交时需要的数据结构,却是这样的:

https://img.mukewang.com/5c7a10e60001c07703250590.jpg

注:client对应的字段,如果是多条并以“;”分隔,则做拆分处理,单条则不处理。

那么如何将数据修改为提交需要的结构?
而对于这种JSON数据结构的拆分,大家有什么解决方案,希望指教一下!


白猪掌柜的
浏览 1309回答 2
2回答

哔哔one

let json =  {    "path": "test",    "clients": [        {            "client": "1.2.2.2;1.1.1.1",            "Access_Type": 2,            "name": "test_01"        },        {            "client": "1.2.2.4;1.1.1.4",            "Access_Type": 1,            "name": "test_02"        },        {            "client": "1.3.3.3",            "Access_Type": 1,            "name": "test_03"        }    ]}let clients = [];json.clients.map(ele1=>{    let {client,Access_Type,name} = ele1;    client.split(';').map(ele2=>{        clients.push({client:ele2,Access_Type,name})    })})json['clients']= clients;

蛊毒传说

&nbsp; &nbsp; var bbb = {&nbsp; &nbsp; &nbsp; &nbsp; "path": "test",&nbsp; &nbsp; &nbsp; &nbsp; "clients":[]&nbsp; &nbsp; };&nbsp; &nbsp; &nbsp;for(var i in aa.clients){&nbsp; &nbsp; &nbsp; &nbsp; var splitarr = aa.clients[i].client.split(";");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var j=0;j<splitarr.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bbb["clients"].push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "client":splitarr[j],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Access_Type":aa.clients[i].Access_Type,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name":aa.clients[i].name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; console.log(bbb);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript