我目前正在制作一个Chrome应用程序,而我正在使用本地存储。然后我的后端返回JSON数据,然后我在本地保存,然后我加密这个JSON中的所有项目。我的问题是我有不同的JSON集,并且还有不同的功能来加密它。
我正在尝试创建动态函数,它将返回这些JSON数据集的副本,但每个项目都已加密,但我无法实现。我的JSON由数组和对象组成。
我的问题是,有可能吗?如果是这样,我需要一份工作样本。
顺便说一下,我正在使用angularJS,这是我目前的代码。
// this function returns a copy of a JSON but with encrypted values.
// encrypts objectData using base 64
// param -> objectData = data from database (rater and its ratees)
fac.encryptData = function(param) {
var raterObject = param;
var resultObject = { 'ratees':[] }; // final value will be equivalent to raterObject
for(property in raterObject) {
if(raterObject[property] instanceof Array) {
angular.forEach(raterObject[property], function(ratee, rateeKey) {
var rateeObject = { 'competencies':[] };
for(property in ratee) {
if(ratee[property] instanceof Array) {
angular.forEach(ratee[property], function(competency, competencyKey) {
var competencyObject = { 'proficiencies':[], 'competencyScoreInfo':[] };
for(property in competency) {
if(competency[property] instanceof Array) {
if(property == 'proficiencies') {
angular.forEach(competency[property], function(proficiency, proficiencyKey) {
var proficiencyObject = {};
for(property in proficiency) {
if(proficiency[property] == null ) {
proficiency[property] = '';
}
if(property != '$$hashKey') {
proficiencyObject[property] = base64.encode(proficiency[property].toString());
} else {
proficiencyObject[property] = proficiency[property];
}
}
competencyObject.proficiencies.push(proficiencyObject);
});
} else if(property == 'competencyScoreInfo') { // di pa tapos
angular.forEach(competency[property], function(proficiency, proficiencyKey) {
var proficiencyObject = { 'indicators':[], 'c1':[], 'c2':[] };
for(property in proficiency) {
if(proficiency[property] instanceof Array) {
if(property == 'indicators') {
angular.forEach(proficiency[property], function(indicator, indicatorKey) {
var indicatorObject = {};
for(property in indicator) {
indicatorObject[property] = base64.encode(indicator[property].toString());
}
proficiencyObject.indicators.push(indicatorObject);
});
} else if (property == 'c1') {
angular.forEach(proficiency[property], function(valueC1, c1Key) {
var c1Object = {};
for(property in valueC1) {
c1Object[property] = base64.encode(valueC1[property].toString());
}
proficiencyObject.c1.push(c1Object);
});
} else if (property == 'c2') {
angular.forEach(proficiency[property], function(valueC2, indicatorKey) {
var c2Object = {};
for(property in valueC2) {
c2Object[property] = base64.encode(valueC2[property].toString());
}
proficiencyObject.c2.push(c2Object);
});
}
} else {
proficiencyObject[property] = base64.encode(proficiency[property].toString());
}
}
competencyObject.competencyScoreInfo.push(proficiencyObject);
});
}
} else {
if(property != '$$hashKey') {
// if(property != '$$hashKey' && property != 'commentSituationTask' && property != 'commentAction' && property != 'commentResult') {
competencyObject[property] = base64.encode(competency[property].toString());
} else {
competencyObject[property] = competency[property];
}
}
}
rateeObject.competencies.push(competencyObject);
});
} else {
if(property != 'currentPage' && property != '$$hashKey') {
rateeObject[property] = base64.encode(ratee[property].toString());
} else {
rateeObject[property] = ratee[property].toString();
}
}
}
resultObject.ratees.push(rateeObject);
});
} else {
resultObject[property] = base64.encode(raterObject[property].toString());
}
}
return resultObject;
};
请参阅,我有这个功能只是加密特定的JSON。然后我有另一个加密函数用于其他JSON,所以我想创建一个动态函数来加密不同的JSON。
答案 0 :(得分:0)
试试这个:
function encryptData(data) {
var encryptedData = null;
if (typeof data == 'object') {
encryptedData = {};
angular.forEach(data, function (value, key) {
encryptedData[key] = encryptData(value);
}
} else if (data instanceof Array) {
encryptedData = [];
for (var i = 0; i < data.length; i++) {
encryptedData.push(encryptData(data[i]));
}
} else {
encryptedData = base64.encode(data.toString());
}
return encryptedData;
}