【问题标题】:How to echo nested JSON object in php如何在 php 中回显嵌套的 JSON 对象
【发布时间】:2017-04-03 20:24:49
【问题描述】:

我试图弄清楚如何从下面的 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
}

【问题讨论】:

  • 能否正确格式化输出的json?目前的状态很难跟上。
  • 你试过echo $arr->location->address1."<br>";吗?
  • 你为什么要$arr->location[1]location 不是(数字)数组,它是一个对象。

标签: php arrays json object


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    $arr = json_decode($json, true);
    

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

    【讨论】:

      【解决方案3】:

      您必须先解码然后调用密钥:

      // 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 语句以获取正确的位置。

      【讨论】:

        猜你喜欢
        • 2015-10-27
        • 2019-07-12
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 2013-03-26
        • 2014-11-24
        • 2019-10-24
        • 2020-05-24
        相关资源
        最近更新 更多