簡體   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