dongmo3413
dongmo3413
2017-04-03 20:24

如何在php中回显嵌套的JSON对象

已采纳

I'm trying to figure out how to echo the address1 out of the JSON below. I've tried this - echo "$arr->location[1]->address1<br>";, but it returns this error

Catchable fatal error: Object of class stdClass could not be converted to string in /home/benrud/public_html/student/webdesign/2016/02_benrud/tinker/data/index.php on line 202.

echo $arr; returns the JSON below.

{
  "photos": [
    "https://s3-media2.fl.yelpcdn.com/bphoto/37El1q8mqM_1tKtQugncZQ/o.jpg",
    "https://s3-media1.fl.yelpcdn.com/bphoto/GLsNPPz5do-_NJktIQvz6w/o.jpg",
    "https://s3-media3.fl.yelpcdn.com/bphoto/Z4rdHERgb10MZgDXnct5lA/o.jpg"
  ],
  "coordinates": {
    "latitude": 33.0479031276,
    "longitude": -117.256002333
  },
  "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/37El1q8mqM_1tKtQugncZQ/o.jpg",
  "is_claimed": false,
  "id": "oscars-mexican-seafood-encinitas-2",
  "review_count": 48,
  "rating": 4.5,
  "hours": [
    {
      "hours_type": "REGULAR",
      "is_open_now": true,
      "open": [
        {
          "is_overnight": false,
          "end": "2100",
          "day": 0,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 1,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 2,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 3,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2200",
          "day": 4,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2200",
          "day": 5,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 6,
          "start": "0800"
        }
      ]
    }
  ],
  "display_phone": "(760) 487-5778",
  "categories": [
    {
      "alias": "seafood",
      "title": "Seafood"
    },
    {
      "alias": "mexican",
      "title": "Mexican"
    }
  ],
  "price": "$",
  "phone": "+17604875778",
  "name": "Oscars Mexican Seafood",
  "location": {
    "zip_code": "92024",
    "address3": null,
    "address1": "115 N El Camino Real",
    "country": "US",
    "city": "Encinitas",
    "state": "CA",
    "cross_streets": "Via Molena & Encinitas Blvd",
    "display_address": [
      "115 N El Camino Real",
      "Encinitas, CA 92024"
    ],
    "address2": ""
  },
  "transactions": [],
  "url": "https://www.yelp.com/biz/oscars-mexican-seafood-encinitas-2?adjust_creative=YqqOIA_bNY3Qb_A1TRMMUg&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_lookup&utm_source=YqqOIA_bNY3Qb_A1TRMMUg",
  "is_closed": false
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douxunzui1519 douxunzui1519 4年前

    Location isn't an array, so I'd imagine just $arr->location->address1.

    点赞 评论 复制链接分享
  • duanbianweng5353 duanbianweng5353 4年前

    You must first decode then call the key so:

    // Decode the JSON STRING
    $arr = json_decode($arr, true); 
    /*
     * first argument is the JSON STRING, 
     * Second sets the flag that the string is a dictionary 
     * (associative array)
     */
    

    Now it is time to call the element. I put it in a conditional so to prevent errors

    if (array_key_exists('address1', $arr['location'])) {
        echo $arr['location']['address1'];
    }
    else {
        echo "Array element Not Found. Here is what I have:
    ";
        print_r($arr);
    }
    

    This Should return your element's value OR Dump the parsed PHP Array for review so you can edit the if statement to get the correct location.

    点赞 评论 复制链接分享
  • dpy3846 dpy3846 4年前
    $arr = json_decode($json, true);
    

    the true parameter makes sure it's an array and not an object

    点赞 评论 复制链接分享

相关推荐