Currently, I am in the process of converting a JavaScript program that utilizes JSON files to one that uses XML files for the settings. The structure of the XML files differs from that of the JSON files, and certain JSON variables need to be hardcoded in the program. To avoid recoding all calls to the JSON data, my approach involves creating objects resembling the JSON data and implementing custom functions to load the XML data into these objects when necessary.
Prior to this project, I had no experience working with JSON. Any suggestions on a more efficient solution are welcome.
For simple JSON files, it has been relatively straightforward to create a corresponding class.
{
"logo": "yes",
"title": "Jordan Pond",
"author": "Matthew Petroff",
"license": 1,
"preview": "../examples/examplepano-preview.jpg",
}
This transforms into:
function config(){
this.logo='yes';
this.title='Jordan Pond';
this.author='Matthew Petroff';
this.license='1';
this.preview='./examples/examplepano-preview.jpg';
}
config = new config();
alert(config.title);
By following this method, all existing calls to the JSON data can proceed as usual. However, I have encountered challenges when dealing with more complex JSON files like:
{
"default": {
"license": 0,
"logo": "no",
"author": "Kvtours.com",
"firstScene": "WilsonRiverFishingHole",
"title": "Wilson"
},
"scenes": {
"pond": {
"title": "Jordan Pond",
"preview": "../examples/examplepano-preview.jpg",
"panorama": "../examples/examplepano.jpg"
}
}
}
I anticipated that this would convert to something along the lines of:
function tourConfig(){
this.default= function() {
license= "0";
logo= "no";
author= "Kvtours.com";
firstScene= "pondCube";
title= "Wilson";
}
this.scenes= function(){
this.pondCube= function() {
this.title= "Jordan Pond (Cube)";
this.preview="examples/examplepano-preview.jpg";
this.panorama="../examples/examplepano.jpg";
}
}
}
tourConfig = new tourConfig();
alert(tourConfig.default.author);
However, I am encountering issues making this work as expected. Does anyone have any thoughts or suggestions on how to resolve this?