简体   繁体   English

使用p5.js在JavaScript中未定义JSON属性

[英]JSON Property undefined in JavaScript with p5.js

I'm just beginning learning to code and this is my first post here so please forgive me if I violate any rules here.... So here is my question, it seems quite simple but I can just not figure out what is the problem. 我刚刚开始学习编码,这是我在这里的第一篇文章,因此,如果我违反这里的任何规则,请原谅我....所以这是我的问题,这似乎很简单,但我无法弄清问题出在哪里。 I have a JSON file like this: 我有一个像这样的JSON文件:

{"imgj":
[
    {
        "id":"1",
        "user_id":"1",
        "tag":"siteplan",
        "imageurl":"images/cb.jpg"          
    },
    {
        "id":"2",
        "user_id":"2",
        "tag":"floorplan",
        "imageurl":"images/cbb.jpg"             
    },
    {
        "id":"3",
        "user_id":"1",
        "tag":"section",
        "imageurl":"images/postit.png"          
    },
    {
        "id":"4",
        "user_id":"2",
        "tag":"siteplan",
        "imageurl":"images/avatar_default.jpg"  
    }
]
}

and in my .js file, I'm trying to get data of img from the JSON file, but always failed. 在我的.js文件中,我试图从JSON文件中获取img的数据,但始终失败。

 p.preload=function(){

                      var url ='imglist.json';
                      imglist = p.loadJSON(url);


                       for(var i=0; i<4; i++) {

                        imgurl = imglist.imgj[i].imageurl;

                        img[i]=p.loadImage("imgurl");
                      }

                    };

The result of the console is shown as below: 控制台的结果如下所示:

                      console.log(typeof imglist == 'object'); //return true
                      console.log(imglist); //return {} imgj:(4)[{...},{...},{...},{...}]
                      console.log(imglist.imgj[1]); //return undefined

it seems that my JSON file is successfully read as an object, but the property of it cannot be read. 看来我的JSON文件已作为对象成功读取,但无法读取其属性。 It's really weird. 真的很奇怪

This should work however it's only fetch workaround. 这应该可以工作,但这只是获取解决方法。 I don't know how to access this not own property made by loadJSON. 我不知道如何访问loadJSON的这个不属于自己的属性。

p.preload= function(){

      var url ='imglist.json';
      fetch(url)
        .then(data=>data.json())
        .then(imglist=>{
             //imglist = p.loadJSON(url);

             console.log(imglist.imgj[0]); // Object { id: "1", user_id: "1", tag: "siteplan", imageurl: "images/cb.jpg" }
             for(let i = 0;i< imglist.imgj.length;i++) {

                 imgurl = imglist.imgj[i].imageurl;

                 img[i]=p.loadImage(imgurl);
             }});

};

After a bunch of fiddling I believe loadJSON is actually asynchronous behind the scenes, so your for-loop continues to execute before the file is done loading. 经过一番摆弄之后,我相信loadJSON实际上在后台是异步的,因此您的for循环会在文件加载完成之前继续执行。

You could store the imglist in a global/classwide variable and do the for-loop inside the setup or draw function instead. 您可以将imglist存储在全局/全类变量中,然后在setupdraw函数中进行for循环。

var imglist;
var imgs;

p.preload = function() {
  var url = 'imglist';
  imglist = loadJSON(url);
}

p.setup = function() {
  imgs = imglist.map(function(imglistitem) { 
    return loadImage(imglistitem.imageurl); 
  });
}

I am not quite sure how p5.js is wired behind the scenes, but the example from the reference uses the draw() function to pick apart the json: https://p5js.org/reference/#/p5/loadJSON 我不太确定如何将p5.js连接到幕后,但是参考文献中的示例使用draw()函数将json分开: https : //p5js.org/reference/#/p5/loadJSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM