Json数组对象取值

2024-08-16 09:54:20 浏览数 (2)

1.数据格式

代码语言:javascript复制
{"data":[{"username":"xxx","avatar":"xxx","gift_name":"xxx"},{"username":"xxx","avatar":"xxx","gift_name":"xxx"},{"username":"xxx","avatar":"xxx","gift_name":"xxx"},{"username":"xxx","avatar":"xxx","gift_name":"xxx"},{"username":"xxx","avatar":"xxx","gift_name":"xxx"}]}

2.取值

代码语言:javascript复制
username   data[0].username
avatar   data[0].avatar

3.vue接口请求后

代码语言:javascript复制
this.getPrizeUser()
        .then(ret => {
          setInterval((i => (() =>{
            if(i>ret.data.length)i=0;
            this.$refs.danmu.render(ret.data[i].avatar,ret.data[i].username,'获得了',ret.data[i].gift_name, 1000);
            i  ;
          }))(0),11000);

        })
        .catch(err => {
            this.$toast.fail("系统开小差,请重试");
        });

0 人点赞