如何使用JavaScript / jQuery在其他JSON数据之后正确添加JSON数据



我一年前开始编程,我对JavaScript/jQuery语言很陌生。我目前正在做一个Web开发人员的实习,我必须使用JSON文件做很多工作。

我使用的 JSON 文件是这样呈现的:

{"sessions": [{"waves": [{"moments": [{ "lat": 43.522182,  "lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "T"},{"lat": 43.522182,"lon": -1.5024,"type": "R"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182, "lon": -1.5024,  "type": "Z"},{ "lat": 43.522182,"lon": -1.5024,"type": "R"}, { "lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "09:46:34","lat": 43.522186,"lon": -1.5024,"level": 0, "distance": 0,"speed": 0.03,"duration": 23.94},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 2,"distance": 60,"speed": 0.53,"duration": 20.01},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 6,"distance": 50,"speed": 1.5,"duration": 25}]}],"date": "05/03/18","time": "09:46:34","lat": 43.522197,"lon": -1.502392,"duration": 5425}

这给了我们这样的东西:结构良好的json文件

此 json 文件包含 1 个冲浪会话。每个冲浪会话都由您在会话期间拍摄的几个波浪组成,每个波浪都以时刻为特征。

我的实习目标之一是创建一个历史json文件,该文件将存储在一个人的每个会话中。因此,每次用户在我工作的网站上上传会话 json 文件时,该新文件的内容都会添加到历史文件的后面。

下面是包含 2 个会话的历史记录 json 文件的示例:

{"sessions":[{"waves":[{"moments":[{"lat":1,"lon":-1,"type":"Z"},{"lat":2,"lon":-2,"type":"T"},{"lat":3,"lon":-3,"type":"R"},{"lat":4,"lon":-4,"type":"Z"},{"lat":5,"lon":-5,"type":"Z"},{"lat":6,"lon":-6,"type":"R"},{"lat":7,"lon":-7,"type":"Z"},{"lat":8,"lon":-8,"type":"Z"}],"direction":"left","date":"05/03/18","time":"09:46:34","lat":43.522186,"lon":-1.5024,"level":0,"distance":0,"speed":2,"duration":23.94},{"moments":[{"lat":9,"lon":-9,"type":"Z"},{"lat":10,"lon":-10,"type":"T"},{"lat":11,"lon":-11,"type":"R"},{"lat":12,"lon":-12,"type":"Z"},{"lat":13,"lon":-13,"type":"Z"},{"lat":14,"lon":-14,"type":"R"},{"lat":15,"lon":-15,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":1,"distance":5,"speed":3,"duration":12},{"moments":[{"lat":16,"lon":-16,"type":"Z"},{"lat":17,"lon":-17,"type":"T"},{"lat":18,"lon":-18,"type":"R"},{"lat":19,"lon":-19,"type":"Z"},{"lat":20,"lon":-20,"type":"Z"},{"lat":21,"lon":-21,"type":"R"},{"lat":22,"lon":-22,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":6,"distance":10,"speed":1,"duration":50},{"moments":[{"lat":23,"lon":-23,"type":"Z"},{"lat":24,"lon":-24,"type":"T"},{"lat":25,"lon":-25,"type":"R"},{"lat":26,"lon":-26,"type":"Z"},{"lat":27,"lon":-27,"type":"Z"},{"lat":28,"lon":-28,"type":"R"},{"lat":29,"lon":-29,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":2,"distance":60,"speed":30,"duration":10},{"moments":[{"lat":30,"lon":-30,"type":"Z"},{"lat":31,"lon":-31,"type":"T"},{"lat":32,"lon":-32,"type":"R"},{"lat":33,"lon":-33,"type":"Z"},{"lat":34,"lon":-34,"type":"Z"},{"lat":35,"lon":-35,"type":"R"},{"lat":36,"lon":-36,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":4,"distance":24,"speed":50,"duration":30}]},{"waves":[{"moments":[{"lat":1,"lon":-1,"type":"Z"},{"lat":2,"lon":-2,"type":"T"},{"lat":3,"lon":-3,"type":"R"},{"lat":4,"lon":-4,"type":"Z"},{"lat":5,"lon":-5,"type":"Z"},{"lat":6,"lon":-6,"type":"R"},{"lat":7,"lon":-7,"type":"Z"},{"lat":8,"lon":-8,"type":"Z"}],"direction":"left","date":"05/03/18","time":"09:46:34","lat":43.522186,"lon":-1.5024,"level":0,"distance":0,"speed":2,"duration":23.94},{"moments":[{"lat":9,"lon":-9,"type":"Z"},{"lat":10,"lon":-10,"type":"T"},{"lat":11,"lon":-11,"type":"R"},{"lat":12,"lon":-12,"type":"Z"},{"lat":13,"lon":-13,"type":"Z"},{"lat":14,"lon":-14,"type":"R"},{"lat":15,"lon":-15,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":1,"distance":5,"speed":3,"duration":12},{"moments":[{"lat":16,"lon":-16,"type":"Z"},{"lat":17,"lon":-17,"type":"T"},{"lat":18,"lon":-18,"type":"R"},{"lat":19,"lon":-19,"type":"Z"},{"lat":20,"lon":-20,"type":"Z"},{"lat":21,"lon":-21,"type":"R"},{"lat":22,"lon":-22,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":6,"distance":10,"speed":1,"duration":50},{"moments":[{"lat":23,"lon":-23,"type":"Z"},{"lat":24,"lon":-24,"type":"T"},{"lat":25,"lon":-25,"type":"R"},{"lat":26,"lon":-26,"type":"Z"},{"lat":27,"lon":-27,"type":"Z"},{"lat":28,"lon":-28,"type":"R"},{"lat":29,"lon":-29,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":2,"distance":60,"speed":30,"duration":10},{"moments":[{"lat":30,"lon":-30,"type":"Z"},{"lat":31,"lon":-31,"type":"T"},{"lat":32,"lon":-32,"type":"R"},{"lat":33,"lon":-33,"type":"Z"},{"lat":34,"lon":-34,"type":"Z"},{"lat":35,"lon":-35,"type":"R"},{"lat":36,"lon":-36,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":4,"distance":24,"speed":50,"duration":30}]}],"date":"05/03/18","time":"09:46:34","lat":43.522197,"lon":-1.502392,"duration":3012}

这会给我们这个:预期的历史记录 json 文件

几天来我一直在尝试很多事情,特别是将我的对象转换为数组,然后使用 push 方法,然后再次将数组返回到对象中,我得到的最接近的一点是:

function toObject(arr) {     //Allows to turn an array into an object
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
Object.size = function(obj) {     //Return the size of the object
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var array=[];
//obj is my new session I want to add in obj2
var obj = {"sessions": [{"waves": [{"moments": [{ "lat": 43.522182,  "lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "T"},{"lat": 43.522182,"lon": -1.5024,"type": "R"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182, "lon": -1.5024,  "type": "Z"},{ "lat": 43.522182,"lon": -1.5024,"type": "R"}, { "lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "09:46:34","lat": 43.522186,"lon": -1.5024,"level": 0, "distance": 0,"speed": 0.03,"duration": 23.94},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 2,"distance": 60,"speed": 0.53,"duration": 20.01},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 6,"distance": 50,"speed": 1.5,"duration": 25}]}],"date": "05/03/18","time": "09:46:34","lat": 43.522197,"lon": -1.502392,"duration": 5425};
array.push(obj);
myJSON = toObject(array);
var myJSON = JSON.stringify(myJSON);
document.getElementById("demo").innerHTML = myJSON;  

//obj 2 is my history file that already contains 1 session
var obj2 = {"sessions":[{"waves":[{"moments":[{"lat":1,"lon":-1,"type":"Z"},{"lat":2,"lon":-2,"type":"T"},{"lat":3,"lon":-3,"type":"R"},{"lat":4,"lon":-4,"type":"Z"},{"lat":5,"lon":-5,"type":"Z"},{"lat":6,"lon":-6,"type":"R"},{"lat":7,"lon":-7,"type":"Z"},{"lat":8,"lon":-8,"type":"Z"}],"direction":"left","date":"05/03/18","time":"09:46:34","lat":43.522186,"lon":-1.5024,"level":0,"distance":0,"speed":2,"duration":23.94},{"moments":[{"lat":9,"lon":-9,"type":"Z"},{"lat":10,"lon":-10,"type":"T"},{"lat":11,"lon":-11,"type":"R"},{"lat":12,"lon":-12,"type":"Z"},{"lat":13,"lon":-13,"type":"Z"},{"lat":14,"lon":-14,"type":"R"},{"lat":15,"lon":-15,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":1,"distance":5,"speed":3,"duration":12},{"moments":[{"lat":16,"lon":-16,"type":"Z"},{"lat":17,"lon":-17,"type":"T"},{"lat":18,"lon":-18,"type":"R"},{"lat":19,"lon":-19,"type":"Z"},{"lat":20,"lon":-20,"type":"Z"},{"lat":21,"lon":-21,"type":"R"},{"lat":22,"lon":-22,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":6,"distance":10,"speed":1,"duration":50},{"moments":[{"lat":23,"lon":-23,"type":"Z"},{"lat":24,"lon":-24,"type":"T"},{"lat":25,"lon":-25,"type":"R"},{"lat":26,"lon":-26,"type":"Z"},{"lat":27,"lon":-27,"type":"Z"},{"lat":28,"lon":-28,"type":"R"},{"lat":29,"lon":-29,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":2,"distance":60,"speed":30,"duration":10},{"moments":[{"lat":30,"lon":-30,"type":"Z"},{"lat":31,"lon":-31,"type":"T"},{"lat":32,"lon":-32,"type":"R"},{"lat":33,"lon":-33,"type":"Z"},{"lat":34,"lon":-34,"type":"Z"},{"lat":35,"lon":-35,"type":"R"},{"lat":36,"lon":-36,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":4,"distance":24,"speed":50,"duration":30}]}],"date":"05/03/18","time":"09:46:34","lat":43.522197,"lon":-1.502392,"duration":3012};

var size = Object.size(obj2.sessions);
console.log(size);

array.push(obj2);
myJSON = toObject(array);
myJSON = JSON.stringify(myJSON);
document.getElementById("demo2").innerHTML = myJSON;
<!DOCTYPE html>
<html>
<body>
<h2>Create JSON string from a JavaScript object.</h2>
<p id="demo"></p>
<p id="demo2"></p>
</body>
</html>

但它给了我这个:我的代码结果

那么有人知道如何获取预期的历史 json 文件吗?(图2( 如果我的解释不够好,请随时告诉我,无法理解我想要的东西。 谢谢

您必须将代码更改为以下内容

Size.forEach(function(I,index){
Obj.sessions.push(index);}

现在您正在尝试将 2 会话数组与主数组合并

通过使用array.push,您可以添加旧会话和新会话,它将反映两个会话,如所示图像文件中所示

反映图像的正确答案

function toObject(arr) {     //Allows to turn an array into an object
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
Object.size = function(obj) {     //Return the size of the object
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var array=[];
//obj is my new session I want to add in obj2
var obj = {"sessions": [{"waves": [{"moments": [{ "lat": 43.522182,  "lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "T"},{"lat": 43.522182,"lon": -1.5024,"type": "R"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182, "lon": -1.5024,  "type": "Z"},{ "lat": 43.522182,"lon": -1.5024,"type": "R"}, { "lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "09:46:34","lat": 43.522186,"lon": -1.5024,"level": 0, "distance": 0,"speed": 0.03,"duration": 23.94},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 2,"distance": 60,"speed": 0.53,"duration": 20.01},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 6,"distance": 50,"speed": 1.5,"duration": 25}]}],"date": "05/03/18","time": "09:46:34","lat": 43.522197,"lon": -1.502392,"duration": 5425};
array.push(obj);
myJSON = toObject(array);
var myJSON = JSON.stringify(myJSON);
document.getElementById("demo").innerHTML = myJSON;  

//obj 2 is my history file that already contains 1 session
var obj2 = {"sessions":[{"waves":[{"moments":[{"lat":1,"lon":-1,"type":"Z"},{"lat":2,"lon":-2,"type":"T"},{"lat":3,"lon":-3,"type":"R"},{"lat":4,"lon":-4,"type":"Z"},{"lat":5,"lon":-5,"type":"Z"},{"lat":6,"lon":-6,"type":"R"},{"lat":7,"lon":-7,"type":"Z"},{"lat":8,"lon":-8,"type":"Z"}],"direction":"left","date":"05/03/18","time":"09:46:34","lat":43.522186,"lon":-1.5024,"level":0,"distance":0,"speed":2,"duration":23.94},{"moments":[{"lat":9,"lon":-9,"type":"Z"},{"lat":10,"lon":-10,"type":"T"},{"lat":11,"lon":-11,"type":"R"},{"lat":12,"lon":-12,"type":"Z"},{"lat":13,"lon":-13,"type":"Z"},{"lat":14,"lon":-14,"type":"R"},{"lat":15,"lon":-15,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":1,"distance":5,"speed":3,"duration":12},{"moments":[{"lat":16,"lon":-16,"type":"Z"},{"lat":17,"lon":-17,"type":"T"},{"lat":18,"lon":-18,"type":"R"},{"lat":19,"lon":-19,"type":"Z"},{"lat":20,"lon":-20,"type":"Z"},{"lat":21,"lon":-21,"type":"R"},{"lat":22,"lon":-22,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":6,"distance":10,"speed":1,"duration":50},{"moments":[{"lat":23,"lon":-23,"type":"Z"},{"lat":24,"lon":-24,"type":"T"},{"lat":25,"lon":-25,"type":"R"},{"lat":26,"lon":-26,"type":"Z"},{"lat":27,"lon":-27,"type":"Z"},{"lat":28,"lon":-28,"type":"R"},{"lat":29,"lon":-29,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":2,"distance":60,"speed":30,"duration":10},{"moments":[{"lat":30,"lon":-30,"type":"Z"},{"lat":31,"lon":-31,"type":"T"},{"lat":32,"lon":-32,"type":"R"},{"lat":33,"lon":-33,"type":"Z"},{"lat":34,"lon":-34,"type":"Z"},{"lat":35,"lon":-35,"type":"R"},{"lat":36,"lon":-36,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":4,"distance":24,"speed":50,"duration":30}]}],"date":"05/03/18","time":"09:46:34","lat":43.522197,"lon":-1.502392,"duration":3012};
//clear array since array already contains the obj 
var array = [] ;
var size = Object.size(obj2.sessions);
console.log(size);
//now add the waves to obj2 in order to format as required
obj.sessions.forEach(function(index,object){ 
obj2.sessions.push(object);
}
array.push(obj2);
myJSON = toObject(array);
myJSON = JSON.stringify(myJSON);
document.getElementById("demo2").innerHTML = myJSON;

添加 plunker 链接 https://plnkr.co/edit/UNWbyDlifsJepNXXcQMI

如果您只想将会话数组合并到主历史记录中,那么使用Array#push()

masterObject.sessions.push(newObj.sessions)

最新更新