Easiest way to interate over a complex JSON object via Javascript

后端 未结 7 1865
生来不讨喜
生来不讨喜 2020-12-21 07:19

I\'m consuming JSON data that has a bit of a weird structure for example:

{
    \"RESULT\": 
    {
        \"COLUMNS\": [\"ID\",\"name\",\"ENABLED\",\"perms\         


        
7条回答
  •  隐瞒了意图╮
    2020-12-21 08:01

    Try this using underscorejs.

    var plain = {
        "RESULT": 
        {
            "COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
            "DATA": [
                    [7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
                    [15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
            ]
        },
        "ERROR": 0
    }
       , formatted = [];
    
    _.each(plain.RESULT.DATA, function(value) {
        var tmp = {};
        _.each(value, function(parameter, pos) {
            tmp[plain.RESULT.COLUMNS[pos]] = parameter;
        });
        formatted.push(tmp);
    });
    
    console.log(formatted);
    

    http://jsfiddle.net/kxR88/

提交回复
热议问题