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

时间:2017-04-03 20:24:49

标签: php arrays json object

我试图找出如何回应下面JSON中的address1。我已尝试过此操作 - echo "$arr->location[1]->address1<br>";,但会返回此错误

  

捕获致命错误:第202行的/home/benrud/public_html/student/webdesign/2016/02_benrud/tinker/data/index.php中无法将类stdClass的对象转换为字符串。

echo $arr;返回以下JSON。

{
  "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 个答案:

答案 0 :(得分:2)

位置不是数组,所以我想象$arr->location->address1

答案 1 :(得分:0)

$arr = json_decode($json, true);

true参数确保它是一个数组而不是一个对象

答案 2 :(得分:-1)

您必须首先解码然后调用密钥,以便:

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

现在是时候调用元素了。我把它放在条件中以防止错误

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

这应返回元素的值或转储已解析的PHP数组以供查看,以便您可以编辑if语句以获取正确的位置。