seblondres Posted November 29, 2014 Share Posted November 29, 2014 Hi, I can't figure out how to access the followings items: redemptionLocations > name from this feed: https://partner-int-api.groupon.com/deals.json?country_code=uk&tsToken=IE_AFF_0_201236_212556_0&division_id=london&offset=0&limit=20 I have tried: data.options.redemptionLocations[0].namedata.options[0].redemptionLocations.namedata.redemptionLocations[0].name Any suggestions? Many Thanks, Link to comment Share on other sites More sharing options...
Ingolme Posted November 29, 2014 Share Posted November 29, 2014 Have you read the API documentation? Just determine which elements are arrays and which are objects. It's not that difficult. Object properties are accessed as object.property. Array elements are accessed as array[index] where index is a number. If the element of an array is an object, you can access the properties of the object: array[0].property Objects are written as object : { property1 : "value1", property2 : 2} Arrays are written as array : [ "value1", 2] Arrays can contain objects, objects can contain arrays. Here's an example of nested objects and arrays: { array1 : [ { property1 : 1, property2 : 2 }, { property1 : 2, property2 : 4 } ], array2 : [ 1, 2 ]} Link to comment Share on other sites More sharing options...
seblondres Posted November 30, 2014 Author Share Posted November 30, 2014 Thanks for your reply. I can retrieve the data from some items such as: data.options[0].value.formattedAmount But I still don't understand how to retrieve an array. based on your example, this should works: data.redemptionLocations[0].name but it doesn't Link to comment Share on other sites More sharing options...
Ingolme Posted November 30, 2014 Share Posted November 30, 2014 What happens when you try it? "Doesn't work" can mean a variety of things, check your browser's Javascript console and see what messages show up. Perhaps the redemptionLocations array is empty, in which case there's no element [0] to access. Can you point me to the documentation for this web service? Another thing to note is that arrays can have zero or more values, and the index starts at zero. To look at all the elements of an array you need a loop. Scroll down to "Looping Array Elements" in this page: http://www.w3schools.com/js/js_arrays.asp Link to comment Share on other sites More sharing options...
seblondres Posted November 30, 2014 Author Share Posted November 30, 2014 I got this error in my JS console: Uncaught TypeError: Cannot read property '0' of undefined when I use: data.redemptionLocations[0].name Here is the doc: https://partner-int-api.groupon.com/help/deal-api Feed: https://partner-int-api.groupon.com/deals.json?country_code=UK&tsToken=IE_AFF_0_201236_212556_0&offset=0&limit=20 Link to comment Share on other sites More sharing options...
Ingolme Posted November 30, 2014 Share Posted November 30, 2014 That error message is saying that there's no property redemptionLocations inside data This documentation does not seem to have information about the structure of the response, so I had to go searching through the response myself. The response does contain a redeptionLocations property and it has at least one item in it. Maybe data does not have the value you expected it to. Check the value of data using console.log(data) and seeing what shows up in the console. Link to comment Share on other sites More sharing options...
seblondres Posted December 3, 2014 Author Share Posted December 3, 2014 Found it: data.options[0].redemptionLocations[0].city Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now