【问题标题】:How to parse through JSON using PHP如何使用 PHP 解析 JSON
【发布时间】:2017-02-08 09:31:13
【问题描述】:

从 Google Maps API 获取地点详细信息时,我无法从返回的 JSON 中检索值。这是我要解析的特定 JSON:

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "48",
            "short_name" : "48",
            "types" : [ "street_number" ]
         },
         {
            "long_name" : "Pirrama Road",
            "short_name" : "Pirrama Road",
            "types" : [ "route" ]
         },
         {
            "long_name" : "Pyrmont",
            "short_name" : "Pyrmont",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "NSW",
            "short_name" : "NSW",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "AU",
            "short_name" : "AU",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "2009",
            "short_name" : "2009",
            "types" : [ "postal_code" ]
         }
      ],
      "adr_address" : "5,
        \u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e,
        \u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e
        \u003cspan class=\"region\"\u003eNSW\u003c/span\u003e
        \u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e,
        \u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
      "formatted_address" : "48 Pirrama Road, Pyrmont NSW, Australia",
      "formatted_phone_number" : "(02) 9374 4000",
      "geometry" : {
         "location" : {
           "lat" : -33.8669710,
           "lng" : 151.1958750
         },
         "viewport" : {
            "northeast" : {
               "lat" : -33.8665053,
               "lng" : 151.1960371
            },
            "southwest" : {
               "lat" : -33.8669293,
               "lng" : 151.1952183
            }
         }
      },
      "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
      "id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
      "international_phone_number" : "+61 2 9374 4000",
      "name" : "Google Sydney",
      "place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
      "scope" : "GOOGLE",
      "alt_ids" : [
         {
            "place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
            "scope" : "APP"
         }
      ],
      "rating" : 4.70,
      "reference" : "CnRsAAAA98C4wD-VFvzGq-KHVEFhlHuy1TD1W6UYZw7KjuvfVsKMRZkbCVBVDxXFOOCM108n9PuJMJxeAxix3WB6B16c1p2bY1ZQyOrcu1d9247xQhUmPgYjN37JMo5QBsWipTsnoIZA9yAzA-0pnxFM6yAcDhIQbU0z05f3xD3m9NQnhEDjvBoUw-BdcocVpXzKFcnMXUpf-nkyF1w",
      "reviews" : [
         {
            "aspects" : [
               {
                  "rating" : 3,
                  "type" : "quality"
               }
            ],
            "author_name" : "Simon Bengtsson",
            "author_url" : "https://plus.google.com/104675092887960962573",
            "language" : "en",
            "rating" : 5,
            "text" : "Just went inside to have a look at Google. Amazing.",
            "time" : 1338440552869
         },
         {
           "aspects" : [
              {
                 "rating" : 3,
                 "type" : "quality"
              }
             ],
            "author_name" : "Felix Rauch Valenti",
            "author_url" : "https://plus.google.com/103291556674373289857",
            "language" : "en",
            "rating" : 5,
            "text" : "Best place to work :-)",
            "time" : 1338411244325
         },
         {
           "aspects" : [
              {
                 "rating" : 3,
                 "type" : "quality"
              }
             ],
            "author_name" : "Chris",
            "language" : "en",
            "rating" : 5,
            "text" : "Great place to work, always lots of free food!",
            "time" : 1330467089039
         }
      ],
      "types" : [ "establishment" ],
      "url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
      "vicinity" : "48 Pirrama Road, Pyrmont",
      "website" : "http://www.google.com.au/"
   },
   "status" : "OK"
}

我正在尝试特别从 name 和 formatted_address 键中检索数据。

我已经尝试了以下方法(例如上面的 json 文件被命名为 practice.json):

$jsondata = file_get_contents("practice.json");
$fileDecode = json_decode($jsondata);
echo $fileDecode->result->name;

但我无法获得任何输出。非常感谢任何帮助!

编辑:这是直接取自 Google Maps API 的第二个 json,也无法从以下位置检索数据:

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "Al Olaya",
            "short_name" : "Al Olaya",
            "types" : [ "sublocality_level_1", "sublocality", "political" ]
         },
         {
            "long_name" : "Riyadh",
            "short_name" : "Riyadh",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Riyadh Province",
            "short_name" : "Riyadh Province",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "Saudi Arabia",
            "short_name" : "SA",
            "types" : [ "country", "political" ]
         }
      ],
      "adr_address" : "\u003cspan class=\"region\"\u003eAl Olaya\u003c/span\u003e, \u003cspan class=\"locality\"\u003eRiyadh\u003c/span\u003e \u003cspan class=\"country-name\"\u003eSaudi Arabia\u003c/span\u003e",
      "formatted_address" : "Al Olaya, Riyadh Saudi Arabia",
      "geometry" : {
         "location" : {
            "lat" : 24.6955386,
            "lng" : 46.6850215
         },
         "viewport" : {
            "northeast" : {
               "lat" : 24.716452,
               "lng" : 46.7034227
            },
            "southwest" : {
               "lat" : 24.6681904,
               "lng" : 46.6635758
            }
         }
      },
      "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
      "id" : "04d055d65120a302eeedbdce0e8b570cbb2026ed",
      "name" : "Al Olaya",
      "photos" : [
         {
            "height" : 1667,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/117088877528560272504/photos\"\u003eHussam abdul nour\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAALzzt2l80CpXQn_Pg2DI9bOxynSSRyNQw4lFes4CL4SNmfl_4a-o1zgmpoK8O-1Jp8Rh08XQg3nlx0mO0JW3-h8xnIkV6D42SEjM-wx6t_ha3IclwpJC6hEtZzEcWXWcCsNyiuvDZWik-SYo9Ae2z3RsLzfRREumciDDmQflcpioEhCTeD-0tjzVt0hFAD_so7uqGhTwRrigrOfOa04qCcRhDN24v2J2kA",
            "width" : 2500
         },
         {
            "height" : 2448,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/101149623891738396593/photos\"\u003eCompssaiŋoɑter\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAANsdQ2VD-tuohl7udmcLHReWz87P9ZoZTM_L8krBB4jaAYlPQc4tdygrUnZ6fXEUvF4kwFFrFUswRf9ObP8QqdplDY93Nf_dg8pcloVx0iRpOwPW7yv18WV1o1vB0vvF4HytfiK2KWymPqrhZ8NZ-8RYN9rc_idToVoByvYHqFtWEhAXkX47Lb2vcBIVdZjG9PEaGhSsenjXTjqzphIdN-oMeG3zKotU3A",
            "width" : 3264
         },
         {
            "height" : 960,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/110505115125440575608/photos\"\u003eam an\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAALFDIa3AeXB62bvuL9V8s79PJA5-f6cW4j2FJvZ8RAcekWkeLDh0b_T13kzL0o7jPbpppfqIi4DjYueD20c84d3Nr9FoS8-YM1YGiqgITw_chOacDIkYlgNpGVA1Lh6DQKJDqtAl6Rr8nzWATUDRjboLRjx5rZFhMmpUEg2Z1nLwEhBalGiaH7g3t1CnkRWEekJQGhREdC4UgB_NfivkK-Sb0qyXViCEWw",
            "width" : 1280
         },
         {
            "height" : 3024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/102067930211195473360/photos\"\u003eف العلي\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAAH4vqs9NYfw3LhRyzS0JUjU0DMLwJUsv7KLYQ4rk74U0pG3yxHPaucQ3o-ehc5tGa4sg6dKH0gCSmPYBjRbuPN4qihitm2jgWCVvsHi4kG0RmSX_95S-WvQ_SC-EBUNzLft8vkAhVY_WzhWv_tnNdYcE6eFj3td0q01IcTktz6rmEhC_M8Ut3tYf3xFHyf74YtO8GhTEUse64pmo0nzXJViSWS3kIgXrGQ",
            "width" : 4032
         },
         {
            "height" : 1024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/113204813794170333862/photos\"\u003eNadine Ellahib\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAAHe5--s72SnRkwOdrUh7Jjd00uyaHqblIOe0JA8cHuSKfKfWvkbFLiQYgLyEzJ9uPTxBv8owv_mYlP3QPypdVi2Z9KWzhlDO9-9uMK6uG22rdDythbLAsXmqtSo30Youd1fYUDI5TyGAk4_GZLBpab79r54mwbE46T7yjooRMOvFEhAeEWrwH9dLg_fX96wFZG7CGhSsaVTzbFBaGb2DMyuTWVtYQIXf1g",
            "width" : 1280
         },
         {
            "height" : 960,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/115381450017292739172/photos\"\u003ebayt rafal\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAAPLiE2rnNB1oKu9sGeppOBkkSFaNMgIqHICb-dJNxvNYNdsSRRnpvIejKWWhqBlWMqCWM4ckOvaQ-QpoxsUyrwUuJxi0IQ1ApnpWBqwdYzQAJm38rnh-afPsdY2V7jPnlGnPRs1hKKNbnP1uVnvdmBi-3_Pl5X7DtX-na3Vgt7-vEhDmDvgjrRqDcDsgdWbN9wFdGhSNt9vXR-DDKfCsJpxXEYIm_wQz2A",
            "width" : 1280
         },
         {
            "height" : 4912,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/117800400423280078042/photos\"\u003eAli Ali\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAAGXi7GJE3Ms5sI6ODIAW6EdUETVTK3USk5Y5_eUyrh_T2jgrHqMr1oYvPy8GWhdn5k9HB71n-og2wVPGISxds9yWvM_GG_qOYJMT0FtBZHKl9PMRyyJMrPQPpenpz_Y6qQ1ExegDrlP_9vCSB-i7SL5H-0_Bd8FkjAobhnLakhleEhC288Fky3Mzp3z640Xu1YgBGhQXZYU-KJS7yFB3bd5Pa_ah4HtHyw",
            "width" : 7360
         },
         {
            "height" : 460,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/111425848650488622103/photos\"\u003eMahmud Hamdy\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAADPfaci3PMLYTTE8KneDYP4nezEamTSDtZjjnE-dU7TlHUipryBG-5_kf5efH8CZp97utlUwJDLyC72gXC7FVTo-m6OOKL-nUTXvTnDuVBfI-0O5r1Lk7xI00-PwiRLy8ZjnzrSKY8wAUwb5ik3K5BMqArhGQTnXb40BXkA5ao9DEhCUPocB0i3SD5K-13UZgqQ-GhQJu2b1lOsZWwRdU5dZBnEMOXNmeQ",
            "width" : 1024
         },
         {
            "height" : 2976,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/116747951557154073650/photos\"\u003eAbo Osamah\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAAPuy2BrljeilT3ZVivEyn0fxNm4aS1OBMIui1ty57-CHk0D_fUQ9B9PQqL7dVhvGE-GFkNBPpsBz8N1Hu0NIMfcjv0x8T7OBPY5vKEGHcG3LDx2xEzy7nU214wqK7HAlj06b6704CoHtQ7aTVZeVD6H910yv9ARCVbdTWKMuym05EhCuQ44VEJtjjsHcCIAZCY1qGhSUcNgdgsMUF3LnahV1O9Cv5q3B8w",
            "width" : 3968
         },
         {
            "height" : 1440,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/105026382392779621412/photos\"\u003eSalahuddin saleem\u003c/a\u003e"
            ],
            "photo_reference" : "CoQBdwAAANsGbEK1sqRiTVRbDczYQBpN7skqGJlSZsXYgwEeqPLCIjStMIxNjNli2wA-oBzkRjQk4Jy_yeGMlXizjEw1CXSbgMDfvNz-z8MpHG-bwAH9SHbVHZa6xnyVzzEpTLfyW2BxdeWjQVBRqJrZLin1TrtLlCqQpOlkzcy5SBwL8NwvEhCZVoZzlsDAW4Bqam01E-4eGhSx4Yvqn96VWxLmDc-OpDhj48ByCA",
            "width" : 2560
         }
      ],
      "place_id" : "ChIJF7QkuDsDLz4R0rJ4SsxFl9w",
      "reference" : "CmRbAAAAwmMaielR1fUd5dDwGvebSvjTkTqtv_KiDPFdKzMneku63kc0V4Uqe15jJRmYsuxh8Mv33fef494zUk7Ip3rl5g7DSHgh7EU4XfG9VhYkMPGgURb08oXLDlBYi1xJTd9OEhDcvi55Sziz3I-IjojRCPrvGhQs-g47WwHHvR36JPrkmUxoM3k_ww",
      "scope" : "GOOGLE",
      "types" : [ "sublocality_level_1", "sublocality", "political" ],
      "url" : "https://maps.google.com/?q=Al+Olaya,+Riyadh+Saudi+Arabia&ftid=0x3e2f033bb824b417:0xdc9745cc4a78b2d2",
      "utc_offset" : 180,
      "vicinity" : "Al Olaya"
   },
   "status" : "OK"
}

【问题讨论】:

  • 请贴一些代码
  • 使用json_decode($json, true) 并读取数组
  • @OscarZarrus 我发布了一些代码。我已经完成了许多 json 解码示例,我知道这似乎微不足道,但由于某种原因,我无法使用我发布的特定 json 检索数据。
  • @MASIDDIQUI 我已经尝试过了,也尝试将它作为我上面指定的对象访问,但我无法检索数据。
  • 您的 json 的 json_encode 返回 NULL,因此您的 json 数据有一些错误

标签: php json google-maps parsing decode


【解决方案1】:

这些行有错误.. 更正 Json 文件..

使用此链接在 practice.json 文件中获取错误http://jsonlint.com/

"adr_address" : "5,
        \u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e,
        \u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e
        \u003cspan class=\"region\"\u003eNSW\u003c/span\u003e
        \u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e,
        \u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",

【讨论】:

  • 谢谢我发布了另一个 json 例如,我能够检索到我需要的数据。
猜你喜欢
  • 2015-08-12
  • 1970-01-01
  • 2019-12-29
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
相关资源
最近更新 更多