如何创建JSON对象和对象数组?

时间:2012-03-22 17:57:11

标签: javascript json

我想从手机获取数据(geoLocation数据)并将其放入JSON对象并通过网络发送到数据库进行存储。我不确定如何创建此对象,如果它应该是一个对象数组。此外,如何设置对象以在更新时接收新信息以发送到服务器?这是我目前的代码。我正在使用eclipse,phonegap,javascript和JSON。

var JSONID = 0;

    // Create a JSON Object Array
    geoLocJSON ();

    /* This is what I want my Object to look like.
    [   
        {
        "id" : 0,
        "geoLoc" : {
                        "Lat" : "",
                        "Long" : ""
                    },
        "direction" : {
                        "Alt" : "",
                        "Head" : "",
                        "Speed" : ""
                       },
        "Time" : ""
        };
    ]
    */

// onSuccess Geolocation
    function onSuccess(position){
        // Testing for data
        var element = document.getElementById('geolocation');
        element.innerHTML = 
        'Latitude: ' + position.coords.latitude + '<br />' +
        'Longitude: ' + position.coords.longitude + '<br />' +
        'Altitude: ' + position.coords.altitude + '<br />' +
        'Heading: ' + position.coords.heading + '<br />' +
        'Speed: ' + position.coords.speed + '<br />' +
        'Timestamp: ' + new Date(position.timestamp) + '<br />' +
        '<hr />' + element.innerHTML;

        // Puts into JSON Object
        geoLocJSON.id = JSONID;
        geoLocJSON.geoLoc.Lat = position.coords.latitude;
        geoLocJSON.geoLoc.Long = position.coords.longitude;
        geoLocJSON.direction.Alt = position.coords.altitude;
        geoLocJSON.direction.Head = position.coords.heading;
        geoLocJSON.direction.Speed = position.coords.speed;
        geoLocJSON.Time = new Date(position.timestamp);

        // Increments the JSONID
        JSONID++;
    }

这将在收集数据1分钟后发布到服务器,并且JSON对象将被删除,除非POST不成功,然后该对象将本地存储到设备,并在网络再次可用时稍后发布。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

var JSONID = 0;


// Create a JSON Object Array
var geoLocJSON = new Array();

/* This is what I want my Object to look like.
[   
    {
    "id" : 0,
    "geoLoc" : {
                    "Lat" : "",
                    "Long" : ""
                },
    "direction" : {
                    "Alt" : "",
                    "Head" : "",
                    "Speed" : ""
                   },
    "Time" : ""
    };
]
*/

// onSuccess Geolocation
function onSuccess(position){
    // Testing for data
    var element = document.getElementById('geolocation');
    element.innerHTML = 
    'Latitude: ' + position.coords.latitude + '<br />' +
    'Longitude: ' + position.coords.longitude + '<br />' +
    'Altitude: ' + position.coords.altitude + '<br />' +
    'Heading: ' + position.coords.heading + '<br />' +
    'Speed: ' + position.coords.speed + '<br />' +
    'Timestamp: ' + new Date(position.timestamp) + '<br />' +
    '<hr />' + element.innerHTML;

    var myJSON = {
        "id":JSONID,
        "geoLoc":{
            "Lat":position.coords.latitude,
            "Long":position.coords.longitude
        },
        "direction":{
            "Alt":position.coords.altitude,
            "Head":position.coords.heading,
            "Speed":position.coords.speed
        },
        "Time":new Date(position.timestamp
    };

    // Increments the JSONID
    JSONID++;
    geoLocJSON.push(myJSON);
}