jquery - Getting array from Foursquare with getJSON and store as variable -



jquery - Getting array from Foursquare with getJSON and store as variable -

ok, working foursquare's api, , can has single item in pretty easily. can grab name, number, rating, etc...

the problem having getting in array. in other words, if there more 1 item, can't figure out how homecoming of them. code looks like:

<script> function getinfo(venueapi) { $.getjson(venueapi, function(data) { console.log(data); name = data.response.venue.name; phone = data.response.venue.contact.formattedphone; $('#content').empty().append('<h1>' + name + '</h1>', b, '<a href="tel:' + phone + '">' + phone + '</a>'); }); } </script>

the 'venueapi' string api endpoint business looking up.

i've tried using $.each, , i've been searching around google crazy person trying solve this.

to recap, trying grab array foursquare (photos, comments, etc) , store in string.

any help appreciated.

thanks!

edit: here's api phone call triggered getinfo function: onclick="getinfo('https://api.foursquare.com/v2/venues/4fd9e1f9d5fb0913decc6c9c?oauth_token=ycckuvro4j4mzq121e4vtnhbrm4yh0favrow0kq2osj32grv&v=20141012');"

and here json returns: link

updated code:

b = '<br/>'; function getinfo(venueapi) { $.getjson(venueapi, function(data) { $(function() { var arr = []; var photoarr = []; function getinfo() { console.log(data); arr = data.response.venue; $('#content').append(arr.id + '<br/>'); $('#content').append(arr.name + '<br/>'); photoarr = arr.photos.groups[0].items; photoarrlength = arr.photos.groups[0].items.length; console.log(photoarr); console.log(photoarrlength); $('#content').append('<br/> photos <br/>'); (var = 0; < photoarrlength ; i++) { $('#content').append(photoarr[i].id + '<br>'); } } getinfo(); }); }); }

this returns:

4fd9e1f9d5fb0913decc6c9c eventide oyster co.

photos 51db48a5498eb7682980e774 5071c5b2e4b0559b4dfd60b5 51cdc340498ed3d72134074f 51c9de9e498e6be2f81a19ba 50ecc042e4b0e13343ad9549 51d09fa8498efa529e38b2a5

which info needed. need create sure can replicate , understand it's doing other values need grab.

pls provide illustration call.

one alternative accomplish determine array length , utilize - loop.

https://developer.mozilla.org/de/docs/web/javascript/reference/global_objects/array/length

using jquery, check jquery.each().

http://api.jquery.com/jquery.each/

edit:

example:

foursquare api call:

https://api.foursquare.com/v2/venues/explore?near=berlin&oauth_token=your_oath_key&v=20141018

response here:

http://lab.sourcloud.com/stackoverflow/26435383/response.js

bascially have within callback function getjson.

$(function() { var info = response; var arr = []; function getinfo() { //console.log(data.response.groups[0].items); arr = data.response.groups[0].items; arrlength = data.response.groups[0].items.length; (var = 0; < arrlength ; i++) { $('#content').append(arr[i].venue.name + '<br/>'); } } getinfo(); });

http://jsfiddle.net/iambnz/wmpfu50w/

answer based on json response:

check - loop.

$(function() { var info = josh; var arr = []; var photoarr = []; function getinfo() { console.log(data); arr = data.response.venue; $('#content').append(arr.id + '<br/>'); $('#content').append(arr.name + '<br/>'); photoarr = arr.photos.groups[0].items; photoarrlength = arr.photos.groups[0].items.length; console.log(photoarr); console.log(photoarrlength); $('#content').append('<br/> photos <br/>'); (var = 0; < photoarrlength ; i++) { $('#content').append(photoarr[i].id + '<br>'); } } getinfo(); });

http://jsfiddle.net/iambnz/tgwvwfrg/

edit 3 (based on answer, code cleanup)

function getinfo(venueapi) { $.getjson(venueapi, function(data) { var arr = []; var photoarr = []; arr = data.response.venue; $('#content').append(arr.name +'<br/>'); photoarr = arr.photos.groups[0].items; photoarrlength = arr.photos.groups[0].items.length; $('#content').append('<br/> photos <br/>') (var = 0; < photoarrlength; i++) { $('#content').append( '<img src="' + photoarr[i].prefix + photoarr[i].width + 'x' + photoarr[i].height + photoarr[i].suffix + '">'); } }); }

jquery arrays json api foursquare

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -