如何在javascript中将json转换为数组?

时间:2016-07-05 11:17:09

标签: javascript arrays json

这是我在javascript中的json我想将其转换为数组。我已经尝试过json stringify和json.parse函数但没有成功。请帮助

{
"1":{
    "1":{
        "id":"3",
        "value":{
            "json":{
                "Temprature":{
                    "key":10
                }
            }
        },
        "date_time":"2016-07-05 11:22:30",
        "dev_id":"1",
        "pro_id":"1"
    },
    "2":{
        "id":"4",
        "value":{
            "json":{
                "Temprature":{
                    "key":20
                }
            }
        },
        "date_time":"2016-07-05 11:24:15",
        "dev_id":"1",
        "pro_id":"1"
    },
    "3":{
        "id":"5",
        "value":{
            "json":{
                "Temprature":{
                    "key":30
                }
            }
        },
        "date_time":"2016-07-05 11:24:22",
        "dev_id":"1",
        "pro_id":"1"
    },
    "4":{
        "id":"6",
        "value":{
            "json":{
                "Temprature":{
                    "key":40
                }
            }
        },
        "date_time":"2016-07-05 11:24:28",
        "dev_id":"1",
        "pro_id":"1"
    },
    "5":{
        "id":"7",
            "value":{
                "json":{
                    "Temprature":{
                        "key":50
                    }
                }
            },
            "date_time":"2016-07-05 11:24:35",
            "dev_id":"1",
            "pro_id":"1"
    },
    "6":{
        "id":"8",
        "value":{
            "json":{
                "Temprature":{
                    "key":60
                }
            }
        },
        "date_time":"2016-07-05 11:24:42",
        "dev_id":"1",
        "pro_id":"1"
    },
    "7":{
        "id":"9",
        "value":{
            "json":{
                "Temprature":{
                    "key":70
                }
            }
        },
        "date_time":"2016-07-05 11:24:49",
        "dev_id":"1",
        "pro_id":"1"
    },
    "8":{
        "id":"10",
        "value":{
            "json":{
                "Temprature":{
                    "key":80
                }
            }
        },
        "date_time":"2016-07-05 11:24:55",
        "dev_id":"1",
        "pro_id":"1"
    },
    "9":{
        "id":"11",
        "value":{
            "json":{
                "Temprature":{
                    "key":90
                }   
            }
        },
        "date_time":"2016-07-05 11:25:02",
        "dev_id":"1",
        "pro_id":"1"
    },
    "10":{
        "id":"12",
        "value":{
            "json":{
                "Temprature":{
                    "key":100
                }
            }
        },
        "date_time":"2016-07-05 11:25:09",
        "dev_id":"1",
        "pro_id":"1"
    }
},
"2":{
    "11":{
        "id":"13",
        "value":{
            "json":{
                "Temprature":{
                    "key":20
                }
            }
        },
        "date_time":"2016-07-05 11:28:29",
        "dev_id":"2",
        "pro_id":"1"
    },
    "12":{
        "id":"14",
        "value":{
            "json":{
                "Temprature":{
                    "key":35
                }
            }
        },
        "date_time":"2016-07-05 11:28:40",
        "dev_id":"2",
        "pro_id":"1"
    },
    "13":{  
        "id":"15",
        "value":{
            "json":{
                "Temprature":{
                    "key":55
                }
            }
        },
        "date_time":"2016-07-05 11:28:47",
        "dev_id":"2",
        "pro_id":"1"
    },
    "14":{
        "id":"16",
        "value":{
            "json":{
                "Temprature":{
                    "key":39
                }
            }
        },
        "date_time":"2016-07-05 11:28:57",
        "dev_id":"2",
        "pro_id":"1"
    },
    "15":{
        "id":"17",
        "value":{
            "json":{
                "Temprature":{
                    "key":49
                }
            }
        },
        "date_time":"2016-07-05 11:29:10",
        "dev_id":"2",
        "pro_id":"1"
    },
    "16":{
        "id":"18",
        "value":{
            "json":{
                "Temprature":{
                    "key":15
                }
            }
        },
        "date_time":"2016-07-05 11:29:19",
        "dev_id":"2",
        "pro_id":"1"
    },
    "17":{
        "id":"19",
        "value":{
            "json":{
                "Temprature":{
                    "key":28
                }
            }
        },
        "date_time":"2016-07-05 11:29:29",
        "dev_id":"2",
        "pro_id":"1"
    },
    "18":{
        "id":"20",
        "value":{
            "json":{
                "Temprature":{
                    "key":17
                }
            }
        },
        "date_time":"2016-07-05 11:29:42",
        "dev_id":"2",
        "pro_id":"1"
    },
    "19":{
        "id":"21",
        "value":{
            "json":{
                "Temprature":{
                    "key":34
                }
            }
        },
        "date_time":"2016-07-05 11:30:11",
        "dev_id":"2",
        "pro_id":"1"
    },
    "20":{
        "id":"22",
        "value":{
            "json":{
                "Temprature":{
                    "key":47
                }
            }
        },
        "date_time":"2016-07-05 11:30:17",
        "dev_id":"2",
        "pro_id":"1"
    }
}

} 期望的输出应该是

Array
(
    [1] => Array
        (
            [1] => stdClass Object
                (
                    [id] => 3
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 10
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:22:30
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [2] => stdClass Object
                (
                    [id] => 4
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 20
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:24:15
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 5
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 30
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:24:22
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [4] => stdClass Object
                (
                    [id] => 6
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 40
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:24:28
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [5] => stdClass Object
                (
                    [id] => 7
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 50
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:24:35
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [6] => stdClass Object
                (
                    [id] => 8
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 60
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:24:42
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [7] => stdClass Object
                (
                    [id] => 9
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 70
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:24:49
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [8] => stdClass Object
                (
                    [id] => 10
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 80
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:24:55
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [9] => stdClass Object
                (
                    [id] => 11
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 90
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:25:02
                    [dev_id] => 1
                    [pro_id] => 1
                )

            [10] => stdClass Object
                (
                    [id] => 12
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 100
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:25:09
                    [dev_id] => 1
                    [pro_id] => 1
                )

        )

    [2] => Array
        (
            [11] => stdClass Object
                (
                    [id] => 13
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 20
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:28:29
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [12] => stdClass Object
                (
                    [id] => 14
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 35
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:28:40
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [13] => stdClass Object
                (
                    [id] => 15
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 55
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:28:47
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [14] => stdClass Object
                (
                    [id] => 16
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 39
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:28:57
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [15] => stdClass Object
                (
                    [id] => 17
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 49
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:29:10
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [16] => stdClass Object
                (
                    [id] => 18
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 15
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:29:19
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [17] => stdClass Object
                (
                    [id] => 19
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 28
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:29:29
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [18] => stdClass Object
                (
                    [id] => 20
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 17
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:29:42
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [19] => stdClass Object
                (
                    [id] => 21
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 34
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:30:11
                    [dev_id] => 2
                    [pro_id] => 1
                )

            [20] => stdClass Object
                (
                    [id] => 22
                    [value] => Array
                        (
                            [json] => Array
                                (
                                    [Temprature] => Array
                                        (
                                            [key] => 47
                                        )

                                )

                        )

                    [date_time] => 2016-07-05 11:30:17
                    [dev_id] => 2
                    [pro_id] => 1
                )

        )

)

我想将其转换为数组。 那我该怎么办? 有什么建议吗?

由于

1 个答案:

答案 0 :(得分:2)

您可以省略密钥。

var data = { "1": { "1": { id: "3", value: { json: { Temprature: { key: 10 } } }, date_time: "2016-07-05 11:22:30", dev_id: "1", pro_id: "1" }, "2": { id: "4", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:24:15", dev_id: "1", pro_id: "1" }, "3": { id: "5", value: { json: { Temprature: { key: 30 } } }, date_time: "2016-07-05 11:24:22", dev_id: "1", pro_id: "1" }, "4": { id: "6", value: { json: { Temprature: { key: 40 } } }, date_time: "2016-07-05 11:24:28", dev_id: "1", pro_id: "1" }, "5": { id: "7", value: { json: { Temprature: { key: 50 } } }, date_time: "2016-07-05 11:24:35", dev_id: "1", pro_id: "1" }, "6": { id: "8", value: { json: { Temprature: { key: 60 } } }, date_time: "2016-07-05 11:24:42", dev_id: "1", pro_id: "1" }, "7": { id: "9", value: { json: { Temprature: { key: 70 } } }, date_time: "2016-07-05 11:24:49", dev_id: "1", pro_id: "1" }, "8": { id: "10", value: { json: { Temprature: { key: 80 } } }, date_time: "2016-07-05 11:24:55", dev_id: "1", pro_id: "1" }, "9": { id: "11", value: { json: { Temprature: { key: 90 } } }, date_time: "2016-07-05 11:25:02", dev_id: "1", pro_id: "1" }, "10": { id: "12", value: { json: { Temprature: { key: 100 } } }, date_time: "2016-07-05 11:25:09", dev_id: "1", pro_id: "1" } }, "2": { "11": { id: "13", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:28:29", dev_id: "2", pro_id: "1" }, "12": { id: "14", value: { json: { Temprature: { key: 35 } } }, date_time: "2016-07-05 11:28:40", dev_id: "2", pro_id: "1" }, "13": { id: "15", value: { json: { Temprature: { key: 55 } } }, date_time: "2016-07-05 11:28:47", dev_id: "2", pro_id: "1" }, "14": { id: "16", value: { json: { Temprature: { key: 39 } } }, date_time: "2016-07-05 11:28:57", dev_id: "2", pro_id: "1" }, "15": { id: "17", value: { json: { Temprature: { key: 49 } } }, date_time: "2016-07-05 11:29:10", dev_id: "2", pro_id: "1" }, "16": { id: "18", value: { json: { Temprature: { key: 15 } } }, date_time: "2016-07-05 11:29:19", dev_id: "2", pro_id: "1" }, "17": { id: "19", value: { json: { Temprature: { key: 28 } } }, date_time: "2016-07-05 11:29:29", dev_id: "2", pro_id: "1" }, "18": { id: "20", value: { json: { Temprature: { key: 17 } } }, date_time: "2016-07-05 11:29:42", dev_id: "2", pro_id: "1" }, "19": { id: "21", value: { json: { Temprature: { key: 34 } } }, date_time: "2016-07-05 11:30:11", dev_id: "2", pro_id: "1" }, "20": { id: "22", value: { json: { Temprature: { key: 47 } } }, date_time: "2016-07-05 11:30:17", dev_id: "2", pro_id: "1" } } },
    array = Object.keys(data).map(function (k) {
        return Object.keys(data[k]).map(function (l) {
            return data[k][l];
        });
    });

console.log(array);

或者你可以保留钥匙。

var data = { "1": { "1": { id: "3", value: { json: { Temprature: { key: 10 } } }, date_time: "2016-07-05 11:22:30", dev_id: "1", pro_id: "1" }, "2": { id: "4", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:24:15", dev_id: "1", pro_id: "1" }, "3": { id: "5", value: { json: { Temprature: { key: 30 } } }, date_time: "2016-07-05 11:24:22", dev_id: "1", pro_id: "1" }, "4": { id: "6", value: { json: { Temprature: { key: 40 } } }, date_time: "2016-07-05 11:24:28", dev_id: "1", pro_id: "1" }, "5": { id: "7", value: { json: { Temprature: { key: 50 } } }, date_time: "2016-07-05 11:24:35", dev_id: "1", pro_id: "1" }, "6": { id: "8", value: { json: { Temprature: { key: 60 } } }, date_time: "2016-07-05 11:24:42", dev_id: "1", pro_id: "1" }, "7": { id: "9", value: { json: { Temprature: { key: 70 } } }, date_time: "2016-07-05 11:24:49", dev_id: "1", pro_id: "1" }, "8": { id: "10", value: { json: { Temprature: { key: 80 } } }, date_time: "2016-07-05 11:24:55", dev_id: "1", pro_id: "1" }, "9": { id: "11", value: { json: { Temprature: { key: 90 } } }, date_time: "2016-07-05 11:25:02", dev_id: "1", pro_id: "1" }, "10": { id: "12", value: { json: { Temprature: { key: 100 } } }, date_time: "2016-07-05 11:25:09", dev_id: "1", pro_id: "1" } }, "2": { "11": { id: "13", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:28:29", dev_id: "2", pro_id: "1" }, "12": { id: "14", value: { json: { Temprature: { key: 35 } } }, date_time: "2016-07-05 11:28:40", dev_id: "2", pro_id: "1" }, "13": { id: "15", value: { json: { Temprature: { key: 55 } } }, date_time: "2016-07-05 11:28:47", dev_id: "2", pro_id: "1" }, "14": { id: "16", value: { json: { Temprature: { key: 39 } } }, date_time: "2016-07-05 11:28:57", dev_id: "2", pro_id: "1" }, "15": { id: "17", value: { json: { Temprature: { key: 49 } } }, date_time: "2016-07-05 11:29:10", dev_id: "2", pro_id: "1" }, "16": { id: "18", value: { json: { Temprature: { key: 15 } } }, date_time: "2016-07-05 11:29:19", dev_id: "2", pro_id: "1" }, "17": { id: "19", value: { json: { Temprature: { key: 28 } } }, date_time: "2016-07-05 11:29:29", dev_id: "2", pro_id: "1" }, "18": { id: "20", value: { json: { Temprature: { key: 17 } } }, date_time: "2016-07-05 11:29:42", dev_id: "2", pro_id: "1" }, "19": { id: "21", value: { json: { Temprature: { key: 34 } } }, date_time: "2016-07-05 11:30:11", dev_id: "2", pro_id: "1" }, "20": { id: "22", value: { json: { Temprature: { key: 47 } } }, date_time: "2016-07-05 11:30:17", dev_id: "2", pro_id: "1" } } },
    array = Object.keys(data).map(function (k) {
        var o = data[k],
            q = {};
        q[k] = Object.keys(o).map(function (l) {
            var p = {};
            p[l] = o[l];
            return p;
        });
        return q;
    });

console.log(array);