0

I have been trying to parse a json array with no success. I can get a root element but not any array elements. Below is the beginning of my json array from Foursquare which has re-occurring venue elements.

 response: {
 keywords: {}
 suggestedRadius: 10000
 headerLocation: "here"
 headerFullLocation: "here"
 headerLocationGranularity: "unknown"
 headerMessage: "Suggestions for Friday evening"
 totalResults: 214
 groups: [
 {
 type: "Recommended Places"
 name: "recommended"
 items: [
 {
 reasons: {
 count: 0
 items: [ ]
 }
 venue: {
 id: "4b799a05f964a520b1042fe3"
 name: "Green Gables"
 contact: {
 phone: "3097472496"
 formattedPhone: "(309) 747-2496"
 }
 location: {
 address: "17485 East 2500 North Rd"

Below is my PHP code to try to get the name of the restaurants.

 $uri = file_get_contents("https://api.foursquare.com/v2/venues/explore?ll=40.7,-89&oauth_token=xxxxxxxxx", true);
 $obj = json_decode($uri, true);
 foreach($obj['response']['groups']['items']['venue'] as $p)
 {']
 if(isset($p['name)) 
 echo $p['name'];
 }

When I execute this code I get an error saying 'Invalid index: venue. If I just use foreach($obj['response']['groups'] as $p) I get results. So it has something to do with determining the name of the elements under groups.

Ok. Here is my latest PHP code. It drills down to the name element and then gives me an error saying "Unefined index: name" and also 'Illegal string offset name". This message appears 14 times which is one time for each item in the array. So why is "name" not recongized? Any ideas.

 foreach($obj['response']['groups'] as $p)
 {
 if(isset($p['items']))
 {
 foreach($p['items'] as $p1)
 {
 if(isset($p1['venue']))
 {
// echo varDumpToString($p1['venue']); // this dump works ok and shows the elements
 foreach($p1['venue'] as $p2)
 {
 echo varDumpToString($p2['name']); // This is where I get the error
 }
 }
 }
 } 
 }
asked Mar 30, 2013 at 14:50
4
  • Is this the complete and verbatim response? If so, it's not even valid JSON. (Missing commas, missing outer level parens, ...) Commented Mar 30, 2013 at 15:18
  • I changed the foreach as follows: foreach($obj['response']['groups']['items']['venue'] as $p) What do you suggest I change? Commented Mar 30, 2013 at 15:59
  • The response you posted is not valid JSON, json_decode cannot work. If you posted it incompletely, you might want to use var_dump to look at the decoded object and derive the correct subscripts from that. Commented Mar 30, 2013 at 16:13
  • WTH is varDumpToString? (But that's irrelevant). My guess: not every venue has a name Commented Mar 30, 2013 at 18:14

1 Answer 1

1

Because you are parsing the JSON object as a PHP array (the second parameter of json_decode), but yet, you are accessing the result as though it was an object.

Either use array subscripts to access the elements ($obj['response']['groups']['items']['venue']), or parse as object (json_decode($uri, false) or json_decode($uri))

answered Mar 30, 2013 at 14:53

1 Comment

I changed my code to foreach($obj['response']['groups']['items']['venue'] as $p) and now it gives me an error saying Undefined index: items. If I remove index, it says the same error for 'groups'. I must be interpreting the json structure wrong or something.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.