2017-12-06 3 views
0

누구나이 일을 도울 수 있다면 궁금합니다. 스크립트 작성에 초보자가 많고 내가 제대로하고 있는지 확실하지 않습니다. 이 JSON 문자열에서 객체를 가져와야하고 "정의되지 않은"오류가 계속 발생합니다. 여기HUBOT Coffescript - JSON obejct

` 
    { 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의 응답에서이 "이미지"개체에 액세스 할 수있는 방법을 궁금가

다음은 JSON입니다.

감사

답변

1

사용자가 콘텐츠에 액세스하기 위해 인덱스를 둘 필요가 귀하의 오브젝트의 데이터 필드는 배열, 즉

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

감사합니다. 이제 "이미지"배열로 이동할 수 있습니다. 하지만 "original.url"필드를 가져 오려고하면 "정의되지 않은"오류가 발생합니다. 여기에 코드가 있습니다 : 'data = JSON.parse (body) console.log data [0] .images [0] .original [0] .url' –

+0

이제는 오타가 생겼습니다. 당신의 도움을 주셔서 감사합니다 –