I am trying to create a JSON Object dynamically using some multiple .each() loops. I tried using .push() but I was only able to get the first "tier" of the JSON object (the first array) populated.
The JS parses through an Excel Spreadsheet (2003/XML) file and needs to output this JSON object "styles" so I can use it to generate CSS on the page(s). So basically, we save an Excel XML Spreadsheet, my JS uses AJAX to "GET" it, then parses through it extracting the styles (and the worksheets/their data). The extraction process should then "dynamically" create this JSON object to be used elsewhere in the JS file.
Here is what I need the JSON to be after the various functions and loops are complete (unless there is a better structure or a structure that makes more sense for my situation)...
var styles = [
"Default": {
"Name": "Normal",
"Style": {
"Alignment": {
"Vertical": "Bottom"
},
"Font": {
"Name": "Calibri",
"Family": "Swiss",
"Size": "11",
"Color": "#000000"
}
}
},
"s57": {
"Name": "Hyperlink",
"Style": {
"Font": {
"Name": "Calibri",
"Family": "Swiss",
"Size": "11",
"Color": "#0066CC",
"Underline": "Single"
}
}
}
]
MY JS (so far)
var styles = []
$(xml).find('Style').each(function(){
var style = {}
var id = $(this).attr('ss:ID');
var type = $(this).children();
type.each(function() {
$.each(this.attributes, function() {
if (this.specified) {
style[this.name] = this.value;
}
});
});
styles.push(style);
console.log(styles);
});
It doesn't work so well. Once I added style[this.name] = this.value, the console was showing a bunch of "X: Object" entries.
SO, how can I generate a JSON object "dynamically" using the .each() and $.each() loops above?
Thanks in advance!
P.S. I have searched quite a bit to try to find an answer to this already. I have found bits and pieces on how to do some of this, but none that populated the object intelligently...
EDIT:
Here is the XML file that I am "parsing":
UPDATE:
I'm getting closer with this:
// Create JSON Object "styles"
var styles = [];
$(xml).find('Style').each(function(){
var style = {};
var id = $(this).attr('ss:ID');
var type = $(this).children();
type.each(function() {
$.each(this.attributes, function() {
if (this.specified) {
style[this.name] = this.value;
}
});
});
//styles[id] = style;
styles.push(style);
});
console.log(styles);
$(document).ready(function(){
$('body').append('<div class="json-output"/>');
$('.json-output').append(JSON.stringify(styles));
});
**JSON.stringify(styles) is now outputting this with the above scripting
[
{
"ss:Vertical":"Bottom",
"ss:FontName":"Calibri",
"x:Family":"Swiss",
"ss:Size":"11",
"ss:Color":"#000000"
},
"ss:FontName":"Calibri",
"x:Family":"Swiss",
"ss:Size":"11",
"ss:Color":"#0066CC",
"ss:Underline":"Single"
},
{
"ss:Horizontal":"Left",
"ss:Vertical":"Center",
"ss:Indent":"1"
}, {
"ss:Vertical":"Center",
"ss:WrapText":"1",
"ss:FontName":"Calibri",
"x:Family":"Swiss",
"ss:Size":"11",
"ss:Color":"#000000",
"ss:Bold":"1"
},
{
"ss:Vertical":"Center",
"ss:WrapText":"1",
"ss:FontName":"Calibri",
"x:Family":"Swiss",
"ss:Size":"11",
"ss:Color":"#008000",
"ss:Bold":"1"
},
{
"ss:Vertical":"Center",
"ss:WrapText":"1"
},
{
"ss:Vertical":"Center",
"ss:WrapText":"1",
"ss:FontName":"Calibri",
"x:Family":"Swiss",
"ss:Size":"11",
"ss:Color":"#808080",
"ss:Bold":"1",
"ss:Pattern":"Solid"
},
{
"ss:Horizontal":"Left",
"ss:Vertical":"Bottom"
},
{
"ss:Horizontal":"Left",
"ss:Vertical":"Center",
"ss:WrapText":"1",
"ss:Format":"0"
},
{
"ss:Horizontal":"Left",
"ss:Vertical":"Center",
"ss:Indent":"1",
"ss:WrapText":"1"
}
]
...
var styles = []' (Swapped the{''s for a '['. I see '[ & ]' used in "JSON" tutorials. How would this be reworked to be "JSON"?style[this.name] = this.value. I got that little gem from another post similar to what I need. I just put it in the $.each() loop and integrated my own stuff (that reads the element's attributes).var styles =is what makes it a literal and not JSON.