【问题标题】:converting javascript program from json to xml将javascript程序从json转换为xml
【发布时间】:2013-11-25 16:16:08
【问题描述】:

我正在将一个使用 json 文件的 javascript 程序重写为使用 xml 文件进行设置的程序。我拥有的 xml 文件与 json 文件和一些我打算硬编码到程序中的 json 变量的结构不同。我认为不重新编码对 json 数据的所有调用是最简单的,所以我试图创建看起来像 json 数据的对象并拥有自己的函数来将 xml 数据加载到需要的对象中。

请注意,我以前从未弄乱过 json。如果您认为可能有更好的方法,我很乐意尝试。

简单的json文件我已经弄明白了,只要做一个看起来一样的类就行了。

 {
"logo": "yes",
"title": "Jordan Pond",
"author": "Matthew Petroff",    
"license": 1,    
"preview": "../examples/examplepano-preview.jpg",
}

变成

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);

这样做,对 json 内容的所有调用都会像往常一样工作。我对更复杂的 json 文件感到困惑,例如

{
"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"
    }
}
}

我认为应该转换成这样的东西。

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);

这是行不通的。关于如何让它工作的任何想法?

【问题讨论】:

    标签: javascript xml json type-conversion


    【解决方案1】:

    您对 javascript 关闭感到困惑。

    我想你想要的如下:

    function tourConfig(){
        var that = this;
        this.default= function() {
            that.license= "0";
            that.logo= "no";
            that.author= "Kvtours.com";
            that.firstScene= "pondCube";
            that.title= "Wilson";
        }
        // as well as scenes method
    }
    

    【讨论】:

    • 这和我的有什么不同?
    猜你喜欢
    • 2017-05-28
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多