繁体   English   中英

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

[英]How to Create a JSON Object and Array of Objects?

我想从电话中获取数据(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不成功),然后将该对象存储在设备本地,然后在网络再次可用时发布。

谢谢您的帮助。

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM