Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从存储在javaScript脚本相同位置的JSON文件创建对象?_Javascript_Json - Fatal编程技术网

如何从存储在javaScript脚本相同位置的JSON文件创建对象?

如何从存储在javaScript脚本相同位置的JSON文件创建对象?,javascript,json,Javascript,Json,我试图让我的应用程序(一个游戏)从存储在服务器上或webapp文件夹中的.json文件加载指定不同级别内容的数据。脚本将这些内容存储在对象“levelcontents”中,每次启动新级别时,都需要完全重新加载该对象。这是加载级别的功能(级别0是主菜单): 因此,例如,当调用loadLevel(0)时,然后调用levelcontents.draw()时,应该执行特定于级别0(游戏从level0.json获得)的draw()函数。 我已经试过一些东西了,但是我找不到任何我可以开始工作的东西。如果有人

我试图让我的应用程序(一个游戏)从存储在服务器上或webapp文件夹中的.json文件加载指定不同级别内容的数据。脚本将这些内容存储在对象“levelcontents”中,每次启动新级别时,都需要完全重新加载该对象。这是加载级别的功能(级别0是主菜单):

因此,例如,当调用loadLevel(0)时,然后调用levelcontents.draw()时,应该执行特定于级别0(游戏从level0.json获得)的draw()函数。 我已经试过一些东西了,但是我找不到任何我可以开始工作的东西。如果有人能解释我到底需要怎么做,最好是用javaScript,我会非常感激

根据要求,level0.json的内容(不太相关):


如果levelcontents是所有级别的数组,则只需执行levelcontents[l].draw(),无需开关语句即可显示
levelcontents
对象的结构。@SergeSeredenko尚未指定结构。我试图根据.json文件的内容指定结构,该文件可以是level0.json、level1.json、level2.json等等,具体取决于参数l的值。
    var levelcontents;
    function loadLevel(l)
    {
        //just ignore the next line
        level = l;

        switch (l){
        case 0://here 'levelcontents' should get the attributes and methods specified in level0.json 
        break;
        case 1://here 'levelcontents should get the attributes and methods specified in level1.json
        break;
        }
    }
{
"draw": function(){
ctx.clearRect(0,0,960,720);
ctx.fillStyle = "#000000";
},
"load": function(){
if (beginning = 0) {erasePrev};
loadHTML('button','level1Button',"position:absolute; top:100px; left:200px;",'LEVEL I','loadLevel(1)',false,false);
loadHTML('a','howtoplayhref',"position:absolute; top:100px; left:100px;",'how to play',false,false,'howtoplay.html');
},
"clear": function(){
removeHTML(level1Button);
removeHTML(howtoplayhref);
}
}