Jump to content

Problem Accessing JSON Items


seblondres

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...