将JSON从一种格式转换为另一种格式

时间:2016-04-18 05:43:45

标签: javascript arrays json

我尝试将以下JSON转换为另一种格式,其中请求JSON选择包含 valueChainEntity 对象名称,并且我还要删除输入输出 array.Note结构是父子关系,输入和输出数组可以出现在items数组中的任何级别。即使在所有级别(但只有内部项目数组) )

var inputJSON = [  
    {  
        "valueChainEntity":{  
            "id":308,
            "valueChainEntityId":48,
            "items":[  
                {  
                    "valueChainEntity":{  
                        "id":309,
                        "valueChainEntityId":49,
                        "items":[  
                            {  
                                "valueChainEntity":{  
                                    "id":312,
                                    "valueChainEntityId":50,
                                    "items":[  

                                    ]
                                },
                                "inputs":[  
                                    {  
                                        "id":312,
                                        "valueChainEntityId":50,
                                        "items":[  

                                        ]
                                    }
                                ],
                                "outputs":[  
                                    {  
                                        "id":313,
                                        "valueChainEntityId":50
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    }
]

var outputJSON=  [

    {  
        "id":308,
        "valueChainEntityId":48,
        "items":[  
            {  
                "id":309,
                "valueChainEntityId":49,
                "items":[  
                    {  
                        "id":312,
                        "valueChainEntityId":50,
                        "items":[  

                        ]
                    }
                ]
            }
        ]
    }
]

我的代码:

 inputJSON.forEach((item) => {
                    return item.valueChainEntity;
                });
//which will traverse only through the first level and will not delete the **inputs** and **outputs** array..

2 个答案:

答案 0 :(得分:1)

看看以下是否有效:



function parseInputJSON(inp) {
        return inp.map(function (item) {
            return getValueChainEntity(item);
        });
    }

function getValueChainEntity(item) {
  if (item['valueChainEntity'] != undefined) {
    var valueChainEntity = item.valueChainEntity;
    valueChainEntity.items = parseInputJSON(valueChainEntity.items);
    return valueChainEntity;
  }
  return item;
}


var inputJSON=[
    {
        "valueChainEntity": {
            "id": 308,
            "valueChainEntityId": 48,
            "items": [
                {
                    "valueChainEntity": {
                        "id": 309,
                        "valueChainEntityId": 49,
                        "items": [
                            {
                                "valueChainEntity": {
                                    "id": 312,
                                    "valueChainEntityId": 50,
                                    "items": []
                                },
                                "inputs": [
                                    {
                                        "id": 312,
                                        "valueChainEntityId": 50,
                                        "items": []
                                    }
                                ],
                                "outputs": [
                                    {
                                        "id": 313,
                                        "valueChainEntityId": 50
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
    }
}];

var newInput = parseInputJSON(inputJSON);

document.getElementById("json").innerHTML = JSON.stringify(newInput, undefined, 4);

<pre id="json"></pre>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是另一种有效的方式..

示例代码

    function populateChildrenRecursively(outputTypeId, valueChainEntities, parentValueChainEntity) {
        for (var i = 0; i < valueChainEntities.length; i++) {
            if (valueChainEntities[i].valueChainEntity.valueChainEntityId != parentValueChainEntity.valueChainEntity.valueChainEntityId && hasInput(outputTypeId, valueChainEntities[i].inputs)) {
                parentValueChainEntity.valueChainEntity.items.push(valueChainEntities[i]);
                if (valueChainEntities[i].outputs.length > 0) {
                    valueChainEntities[i].valueChainEntity.items = [];
                    for (var j = 0; j < valueChainEntities[i].outputs.length; j++) {
                        populateChildrenRecursively(valueChainEntities[i].outputs[j].outputTypeId, valueChainEntities, valueChainEntities[i]);
                    }
                }
            }
        }
    }

https://plnkr.co/edit/d2wtXcaU37quthBjgrCu?p=preview