Les comparto una funcion que utilice para agregar un prefijo a todas las propiedades de un JSON utilizando JavaScript.
function addPrefix(prefix, source) {
if (Object.prototype.toString.call(source) === '[object Array]') {
var clone = [];
for (var i=0; i<source.length; i++) {
clone[i] = addPrefix(prefix, source[i]);
}
return clone;
} else if (typeof(source)=="object") {
var clone = {};
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
clone[prefix + prop] = addPrefix(prefix, source[prop]);
}
}
return clone;
} else {
return source;
}
}
var newJson = addPrefix("pref",{prop1:"hola",prop2:[{prop3:"miguel"}]});
alert(JSON.stringify(newJson)); |
function addPrefix(prefix, source) {
if (Object.prototype.toString.call(source) === '[object Array]') {
var clone = [];
for (var i=0; i<source.length; i++) {
clone[i] = addPrefix(prefix, source[i]);
}
return clone;
} else if (typeof(source)=="object") {
var clone = {};
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
clone[prefix + prop] = addPrefix(prefix, source[prop]);
}
}
return clone;
} else {
return source;
}
}
var newJson = addPrefix("pref",{prop1:"hola",prop2:[{prop3:"miguel"}]});
alert(JSON.stringify(newJson));
Analista/Desarrollador y cofundador de QBit Mexhico. Tecnologías utilizadas Android, Java (EJB, Servlets, Faces, Groovy & Grails, Maven, Swing), .Net (C#, VB, ASP, MVC), Delphi, Spring, Hibernate, DTSx, Oracle, T-SQL, Firebird, MySQL, MongoDB, NodeJs, ReactJs (Flux). Sistemas Operativos Mac, Ubuntu y Windows.