Jump to content
Sign in to follow this  
seblondres

Problem Accessing JSON Items

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,

Share this post


Link to post
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 ]}

Share this post


Link to post
Share on other sites

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 :facepalm:

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...