如何在 PHP 中将 JSON 解析为数组或对象
Oct 21, 2024 pm 02:47 PM使用 PHP 解析 JSON
JSON 是一种流行的数据格式,通常用于在 Web 应用程序和 API 之间交换数据。在 PHP 中,json_decode() 函数可用于将 JSON 字符串解析为 PHP 数组或对象。
要将 JSON 解析为对象,只需使用 $json = json_decode($data) 语法。然后可以使用点表示法访问生成的对象。
但是,如果要将 JSON 解析为数组,则需要将第二个参数 true 传递给 json_decode() 函数。这将返回一个关联数组,其中键是属性名称,值是相应的值。
以以下 JSON 数据为例:
{ "kind": "shopping#products", "etag": "\"T9uPnY2MZMB71TDpKXXZdr3yWX4/qtJ5vmpftFWNfijyLD9ti2Xpj-w\"", "id": "tag:google.com,2010:shopping/products", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d2", "nextLink": "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d5", "previousLink": "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d1", "totalItems": 633694, "startIndex": 2, "itemsPerPage": 3, "currentItemCount": 3, "items": [ { "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/1161353/11882813508247586172", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1161353/gid/11882813508247586172", "product": { "googleId": "11882813508247586172", "author": { "name": "Buy.com", "accountId": "1161353" }, "creationTime": "2011-04-24T05:13:38.000Z", "modificationTime": "2011-08-05T17:45:24.000Z", "country": "US", "language": "en", "title": "Sony BRAVIA KDL-46EX720 46 inch 3D LED HDTV 1080p 120Hz", "description": "Entertainment lovers the slim Sony EX720-Series LED HDTV is for you. See precise motion detail plus watch your favorite 2D or 3D entertainment in clear, vivid Full HD 1080p picture quality with incredible contrast. You can even connect to the internet and access a great selection of online entertainment with Netflix , Hulu Plus , Pandora , Qriocity and more - there s always something on. Plus video chat with friends and family when you Skype on the big screen.", "link": "http://clickfrom.buy.com/default.asp?adid\u003d17902&sURL\u003dhttp%3A%2F%2Fwww.buy.com%2Fprod%2Fsony-bravia-kdl-46ex720-46-3d-led-hdtv-1080p-120hz%2Fq%2Fsellerid%2F10004001%2Floc%2F111%2F219891693.html", "brand": "Sony", "condition": "new", "gtin": "00027242817081", "gtins": [ "00027242817081" ], "inventories": [ { "channel": "online", "availability": "inStock", "price": 1348.0, "currency": "USD" } ], "images": [ { "link": "http://ak.buy.com/PI/0/1000/219891693.jpg" } ] } } ] }
登录后复制
将此 JSON 解析为数组,使用以下语法:
<code class="php">$json = json_decode($data, true);</code>
登录后复制
这将返回以下数组:
[ "kind" => "shopping#products", "etag" => "\"T9uPnY2MZMB71TDpKXXZdr3yWX4/qtJ5vmpftFWNfijyLD9ti2Xpj-w\"", "id" => "tag:google.com,2010:shopping/products", "selfLink" => "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&amp;q\u003dsony&amp;maxResults\u003d3&amp;startIndex\u003d2", "nextLink" => "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&amp;q\u003dsony&amp;maxResults\u003d3&amp;startIndex\u003d5", "previousLink" => "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&amp;q\u003dsony&amp;maxResults\u003d3&amp;startIndex\u003d1", "totalItems" => 633694, "startIndex" => 2, "itemsPerPage" => 3, "currentItemCount" => 3, "items" => [ [ "kind" => "shopping#product", "id" => "tag:google.com,2010:shopping/products/1161353/11882813508247586172", "selfLink" => "https://www.googleapis.com/shopping/search/v1/public/products/1161353/gid/11882813508247586172", "product" => [ "googleId" => "11882813508247586172", "author" => [ "name" => "Buy.com", "accountId" => "1161353" ], "creationTime" => "2011-04-24T05:13:38.000Z", "modificationTime" => "2011-08-05T17:45:24.000Z", "country" => "US", "language" => "en", "title" => "Sony BRAVIA KDL-46EX720 46 inch 3D LED HDTV 1080p 120Hz", "description" => "Entertainment lovers the slim Sony EX720-Series LED HDTV is for you. See precise motion detail plus watch your favorite 2D or 3D entertainment in clear, vivid Full HD 1080p picture quality with incredible contrast. You can even connect to the internet and access a great selection of online entertainment with Netflix , Hulu Plus , Pandora , Qriocity and more - there s always something on. Plus video chat with friends and family when you Skype on the big screen.",
登录后复制
以上是如何在 PHP 中将 JSON 解析为数组或对象的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)