【问题标题】:How to fix 'when trying to get a url from a json file i get undefined'?如何解决“尝试从 json 文件中获取 url 时我未定义”?
【发布时间】:2019-09-29 07:34:01
【问题描述】:

我正在和我的朋友一起为个人服务器制作一个机器人,我正在尝试将它连接到 xboru 的 api https://xbooru.com/index.php?page=help&topic=dapi 问题是,在将 .xml 文件转换为 .json 文件后,当我尝试获取 file_url 时,我得到的结果是 undefined

我已经尝试过一些事情(我对 js 很陌生),比如将 json 转换为 js 数组并控制值

xmlToJson(url, function(err, data) { //from xml to json
        if (err) {
            return console.err(err);
        }
            let json = JSON.stringify(data, null, 2)
            let myArr = JSON.parse(json)
            console.log(json)
            console.log(myArr)
            let post2 = (myArr.posts.post[0])
            console.log(post2.sample_url)//gives undefined
            message.channel.send(post2.sample_url)//error because i can't send empty message
        });

我希望输出是图像 url,但输出是我已经说过的“未定义”

【问题讨论】:

    标签: javascript json discord discord.js


    【解决方案1】:

    找到答案.. 当我在做 console.log(post2.sample_url) 时,我需要添加另一个东西是 '$' 所以它变成了 console.log(post2.$.sample_url)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-06
      • 2021-01-06
      • 2021-01-16
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多