将对象附加到JSON列表

时间:2014-12-22 19:15:00

标签: javascript json

发生异常,我不确定为什么因为我是JS的新手。我一到达testData["beacons"].append(beacon);行,我的代码就会跳转到catch(e)。我假设我不能将对象附加到其他数组?

JS:

$(document).ready(function() {
    // Data to describe what kind of test
    var testData = {
        "timestamp": "",
        "hive": 0,
        "hdfs": 0,
        // Contains a list of testData objects
        "beacons":[]
    };


    var testRun = document.getElementById("test-form");
    testRun.addEventListener('submit', function(event) {
        event.preventDefault();
        var selectedTest = document.querySelector('input[name=test-select]:checked');
        alert(selectedTest);
        var testType = selectedTest.id;
        if (testType == "hdfs-test") {
            testData["hdfs"] = 1;
            testData["hive"] = 0;
        } else if (testType == "hive-test") {
            testData["hdfs"] = 0;
            testData["hive"] = 1;
        } else if (testType == "hdfs-hive-test") {
            testData["hdfs"] = 1;
            testData["hive"] = 1;
        } else {
        //    null
        }

        var events = document.getElementById("event-textarea").value;
        // check in valid input
        var eventSource = events.replace("],[","],,,,[");
        // beaconLists allows users to submit --> [{beacon1}, {beacon2}, ...], [{beacon3}, {beacon4}, ...]
        var beaconLists = eventSource.split(",,,,");
        for (var i = 0; i < beaconLists.length; i++) {
            // inspect one list in beaconLists [{beacon1}, {beacon2}, ...]
            var beaconList = beaconLists[i];
            try {
                // list of JSON objects
                var beaconObjList = JSON.parse(beaconList);
                for (var j = 0; j < beaconObjList.length; j++) {
                    var beaconObj = beaconObjList[j];
                    if (beaconObj["data"] && beaconObj["application"]) {
                    //    successful parse to find events
                    //    describe beacon being tested
                        alert("yes");
                        var beacon = {
                            "app_name": beaconObj["application"]["app_name"],
                            "device": beaconObj["application"]["device"],
                            "device_id": beaconObj["application"]["device_id"],
                            "os": beaconObj["application"]["os"],
                            "os_version": beaconObj["application"]["os_version"],
                            "browser": beaconObj["application"]["browser"],
                            "beacon": beaconObj
                        };
                        // append to testData
                        testData["beacons"].append(beacon);
                        // reset beacon so we can append new beacon later
                        beacon = {};
                    } else {
                    //    notify event isn't in the correct format?
                        alert("no");
                    }
                }
            } catch (e) {
            //     notify bad JSON
                alert("failed");
            }
        }
        console.log(testData);
        //$.ajax({
        //    type: "POST",
        //    url: "/test/",
        //    data: testData,
        //    success: function () {
        //        alert("yay");
        //    },
        //    failure: function () {
        //        alert("boo");
        //    }
        //});

    });

});

2 个答案:

答案 0 :(得分:2)

拥有一个对象数组没有任何问题。 JavaScript处理就好了。主要问题是append是一个用于添加元素的jQuery API方法(psuedo native appendChild)。 push是你添加到数组的方式。

testData["beacons"].push(beacon);

此外,这部分代码存在问题。

// reset beacon so we can append new beacon later
beacon = {};

变量beacon和此处testData["beacons"]添加的变量都相同。在JavaScript中,testData["beacons"]最近beacon的值与变量beacon相同。当变量beacon中的值设置为{}时,数组的值也是如此。只需要删除这行代码。在变量环境设置中,使用var将为每次迭代beacon设置一个新变量。

答案 1 :(得分:1)

您应该使用push方法,例如:

testData["beacons"].push(beacon);