简体   繁体   中英

How to parse a json array?

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
   }
   }
   }
   }   
   }

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) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM