使用Javascript更改JSON的格式

时间:2015-08-17 22:31:33

标签: javascript json

好吧,所以在我的代码中我抓住了一个看起来像这样的JSON blob:

[
    {
        "ts": 1431736740,
        "aggs": {
            "DNS": {
                "min": 20,
                "max": 21,
            },
            "SEND": {
                "min": 102,
                "max": 8114,
            },
            "SSL": {
                "min": 110,
                "max": 7806,
            },
            "SERVERBUSY": {
                "min": 1360,
                "max": 13709,
            }
        }
    },
    {
        "ts": 1431736680,
        "aggs": {
            "DNS": {
                "min": 22,
                "max": 24,
            },
            "SEND": {
                "min": 210,
                "max": 8251,
            },
            "SSL": {
                "min": 117,
                "max": 12488,
            },
            "SERVERBUSY": {
                "mn": 6462,
                "max": 9800,
            }
        }
    },
    {
        "ts": 1431736620,
        "aggs": {
            "DNS": {
                "min": 21,
                "max": 22,
            },
            "SEND": {
                "min": 92,
                "max": 12035,
            },
            "SSL": {
                "min": 111,
                "max": 9758,
            },
            "SERVERBUSY": {
                "min": 9855,
                "max": 14112,
            }
        }
    }
]

我需要帮助将其转换为如下所示:

[
    {
        "key": "DNS",
        "values": [
            [
                0,   //The first number just goes up by one.
                20   //The second number is the value of aggs.DNS.min
            ],
            [
                1,
                22
            ],
            [
                2,
                21
            ]
        ]
    },
    {
        "key": "SEND",
        "values": [
            [
                0,
                102
            ],
            [
                1,
                210
            ],
            [
                2,
                92
            ]
        ]
    },
    {
        "key": "SSL",
        "values": [
            [
                0,
                110
            ],
            [
                1,
                117
            ],
            [
                2,
                111
            ]
        ]
    },
    {
        "key": "SERVERBUSY",
        "values": [
            [
                0,
                1360
            ],
            [
                1,
                6462
            ],
            [
                2,
                9855
            ]
        ]
    }
]

因为我使用的库只使用正确格式的JSON。这可能吗?我发现了一些不同的问题,表明你可以改变JSON格式,但我不确定它是否可以做到这一点。非常感谢任何帮助!!!

1 个答案:

答案 0 :(得分:0)

好吧,我想发布我想出来的内容,以防它帮助其他人:

doubleRef

这不是最漂亮的解决方案,但它可以满足我的需求。