[英]Iterate through nested JSON tree and change values
我所擁有的是以下結構的JSON樹:
{
"projects": {
"Proj1": {
"milestones": {
"default": "20150101",
"default2": "20140406",
"default3": "20140101",
"default4": "20131231",
"default5": "20131220"
}
},
"Proj2": {
"milestones": {
"default": "20131231",
"default2": "20131220"
}
}
}
}
我有將其讀入網頁的代碼,文本中包含“默認”部分,文本框/表單中包含數字/日期。 這個想法是,您可以更改日期並提交,然后提交到后端並寫入文件。 所有這些大部分都起作用。 我能弄清楚的是如何遍歷我擁有的JSON樹並編寫新值。 例如:
訪問JSONTREE.projects.Proj1.milestones.default將返回該鍵的值。 通過該調用設置值會適當更改值。 我要做的是遍歷整個樹,並根據表單框中的內容設置“默認值”的值。 我有這個:
$.each(formJSON.projects, function (projectName) {
$.each(this, function (selection) {
$.each(this, function (milestones, date) {
var saltKey = projectName + "-" + milestones;
date = document.getElementById(saltKey).value;
});
});
});
但即使'alert(date)'返回一個值,它也不會執行任何操作。 我懷疑這是因為它是值,而不是對對象的引用,但是我怎樣才能到達該對象? 我懷疑這很簡單,但是我不是jQuery / JS的專業人士。
TL; DR如何獲取對嵌套JSON樹中的鍵的引用,以便可以更改值?
編輯:好的,我認為這就是我需要的: JS / Jquery-在json選擇器中使用變量 。 我將“日期”部分更改為:formJSON.projects [projectName] [selection] [milestones] = document.getElementById(saltKey).value; 這似乎有效。
我遇到了同樣的問題,這是我的解決方案,可以遍歷JSON對象(MongoDB)並修改某些元素,無論這些元素是對象的屬性還是屬性是包含更多對象的數組。 我知道這個問題很舊,但是對某人可能有用。 假設我們有一個像這樣或更復雜的對象。
var mixData = {
"Hello": "World",
"Foo" : "Bar",
"sudo": [
{ "apt-get": "upgrade" , "force": false },
{ "apt-get": "update" , "force": true}
],
"colors": ["blue","green"],
"numbers":{
"integer": [
{"num": 1},
{"num": 2},
{"num": 3}
],
"operator": "addition"
}
};
而且我們想替換一些字符串(在這種情況下為藍色),但是我們不知道數據在哪里或哪種構造函數。
// Detect if the element is an Array
function isElementArray(element){
return (element.constructor === Array ? true : false);
}
//Detect if the element is an Object
function isElementObject(element){
return (element.constructor === Object ? true : false);
}
現在我們有了這兩個函數,無論對象是對象還是數組,我們都將使用第三個函數遍歷該項目。
function iterate(element){
//Check if the element is an Object
if(isElementObject(element)){
for (var property in element){
if(isElementObject(element[property])){
element[property] = iterate(element[property]);
}else if(isElementArray(element[property])){
//An array
for(var x = 0; x < element[property].length; x++){
element[property][x] = iterate(element[property][x]);
}
}else{
if(element.hasOwnProperty(property)){
console.log("New object inside object property");
element[property] = iterate(element[property]);
}else{
element[property] = replaceElement(element[property].toString());
console.log("Single Element: " + element[property] )
console.log(element + " " + element[property]);
}
}
}
}else if(isElementArray(element)){
//An Array
for (var x = 0; x < element.length; x++){
element[x] = iterate(element[x]);
}
}else{
//Single element in array or property
element = replaceElement(element.toString());
console.log("Single Element : " + element);
}
return element;
}
還有我們需要用來替換字符串的函數。
function replaceElement(element){
if(element === "blue"){
return "Blue is the warmest color"
}else{
return element;
}
}
最后,我們可以獲得結果:
console.log(iterate(mixData));
結果是:
{
"Hello": "World",
"Foo" : "Bar",
"sudo": [
{ "apt-get": "upgrade" , "force": false },
{ "apt-get": "update" , "force": true}
],
"colors": ["Blue is the warmest color","green"],
"numbers":{
"integer": [
{"num": 1},
{"num": 2},
{"num": 3}
],
"operator": "addition"
}
};
您可以根據需要更改替換功能。 當然,請刪除所有控制台日志。
您編輯傳遞的值,而不是原始JSON對象。
解決此問題的一種方法是創建一個新的JSON對象,在遍歷現有對象時進行構建,然后覆蓋原始對象或使用新的JSON對象。
另一個方法是創建一個保存原始JSON對象的變量,然后將其傳遞給函數或直接在函數內部訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.