HUBOT Coffescript

编程入门 行业动态 更新时间:2024-10-24 09:22:31
HUBOT Coffescript - JSON对象(HUBOT Coffescript - JSON obejct)

想知道是否有人可以帮助我 - 这在脚本编程方面非常新手,并且不确定我是否做得对。 我需要从这个JSON字符串中获取一个对象,并且一直给我“未定义”的错误。

这是JSON:

` { data: [ { type: 'gif', id: 'Cmr1OMJ2FN0B2', slug: 'hello-Cmr1OMJ2FN0B2', url: 'https://giphy.com/gifs/hello-Cmr1OMJ2FN0B2', bitly_gif_url: 'https://gph.is/2bZufS7', bitly_url: 'https://gph.is/2bZufS7', embed_url: 'https://giphy.com/embed/Cmr1OMJ2FN0B2', username: '', source: 'https://www.fanpop.com/clubs/penguins-of-madagascar/images/37800672/title/hello-photo', rating: 'g', content_url: '', source_tld: 'www.fanpop.com', source_post_url: 'https://www.fanpop.com/clubs/penguins-of-madagascar/images/37800672/title/hello-photo', is_indexable: 0, import_datetime: '2016-09-05 13:48:36', trending_datetime: '2017-09-19 14:26:18', images: [Object], title: 'bom dia hello GIF' } ], pagination: { total_count: 2516, count: 1, offset: 0 }, meta: { status: 200, msg: 'OK', response_id: '5a28576867382f644dc7d33b' } } `

这是我的HUBOT脚本:

` robot.hear /^(no)$|^.*(\sno\s).*$/i, (res) -> api_url = 'https://api.giphy.com' path = '/v1/gifs/search' url = "#{api_url}#{path}" robot.http(url) .query q: "nono+penguin" rating: 'g' limit: 1 fmt: 'json' .header('api_key', giphyAuthToken) .header('Content-Type', 'application/json') .get() (err, res, body) -> # error checking code here if err console.log err else data = JSON.parse(body) console.log data #this prints above data console.log "success....got giphy response" console.log data.images.original.url #This is giving error that original is undefined process.exit(1) `

想知道如何从Giphy的回应中访问这个“图像”对象。

谢谢

Wondering if anyone can help me with this - pretty much novice in scripting and not sure if I'm doing it right. I need to get an object from this JSON string and it keeps giving me "undefined" error.

Here's the JSON:

` { data: [ { type: 'gif', id: 'Cmr1OMJ2FN0B2', slug: 'hello-Cmr1OMJ2FN0B2', url: 'https://giphy.com/gifs/hello-Cmr1OMJ2FN0B2', bitly_gif_url: 'https://gph.is/2bZufS7', bitly_url: 'https://gph.is/2bZufS7', embed_url: 'https://giphy.com/embed/Cmr1OMJ2FN0B2', username: '', source: 'https://www.fanpop.com/clubs/penguins-of-madagascar/images/37800672/title/hello-photo', rating: 'g', content_url: '', source_tld: 'www.fanpop.com', source_post_url: 'https://www.fanpop.com/clubs/penguins-of-madagascar/images/37800672/title/hello-photo', is_indexable: 0, import_datetime: '2016-09-05 13:48:36', trending_datetime: '2017-09-19 14:26:18', images: [Object], title: 'bom dia hello GIF' } ], pagination: { total_count: 2516, count: 1, offset: 0 }, meta: { status: 200, msg: 'OK', response_id: '5a28576867382f644dc7d33b' } } `

And here's my HUBOT script:

` robot.hear /^(no)$|^.*(\sno\s).*$/i, (res) -> api_url = 'https://api.giphy.com' path = '/v1/gifs/search' url = "#{api_url}#{path}" robot.http(url) .query q: "nono+penguin" rating: 'g' limit: 1 fmt: 'json' .header('api_key', giphyAuthToken) .header('Content-Type', 'application/json') .get() (err, res, body) -> # error checking code here if err console.log err else data = JSON.parse(body) console.log data #this prints above data console.log "success....got giphy response" console.log data.images.original.url #This is giving error that original is undefined process.exit(1) `

Wondering how can I access this "images" object from Giphy's response.

Thanks

最满意答案

对象中的数据字段是一个数组,因此您需要放置索引以访问内容,即

data = JSON.parse(body) console.log data[0].images

The data field in your object is an Array so you need to put the index in order to access the content, i.e.

data = JSON.parse(body) console.log data[0].images

更多推荐

本文发布于:2023-08-07 21:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HUBOT   Coffescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!