我正在尝试从Google购物API中提取数据。我能够成功下载数据,但是在使用PHP解析数据时遇到了麻烦。我仍在学习,但是我似乎在多维数组方面遇到问题。我使用捕获JSON
$json = json_decode($data);
。
以下只是呼应外部数组,但我不能从内部数组中提取:
foreach($json as $key => $value) {
echo $key . " : " . $value;
}
如果要获取每个产品的“标题”,“描述”,“品牌”和“可用性”,我将如何解析?
{
"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"
}
]
}
},
{
"kind": "shopping#product",
"id": "tag:google.com,2010:shopping/products/7827/1976288072671326122",
"selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/7827/gid/1976288072671326122",
"product": {
"googleId": "1976288072671326122",
"author": {
"name": "Dell",
"accountId": "7827"
},
"creationTime": "2011-06-08T15:11:49.000Z",
"modificationTime": "2011-08-05T15:04:13.000Z",
"country": "US",
"language": "en",
"title": "Sony Bravia 55\" KDL55EX500 1080p 120Hz LCD HDTV",
"description": "Enjoy breathtaking viewing experience with Bravia 55-inch KDL55EX500 1080p LCD HDTV from Sony®. This LCD TV features Motionflow™ 120 Hz Technology that allows you to experience smooth motion detail and clarity which delivers a crisp, fluid and realistic viewing skill during fast-action scenes. Additionally, the incorporated LightSensor™ Technology automatically increases or decreases the level of brightness of your TV's backlight based on a room's lighting conditions. Moreover, the BRAVIA Engine™ 2 fully processor lets you enjoy sharp, vibrant, life-like images while optimizing color, contrast and significantly reducing noise. Besides this, get a natural looking picture with an Ambient Sensor. Plus, you can also share your photos and music on the big screen by simply connecting your digital camera, USB-enabled MP3 player, or USB storage device directly to your HDTV's USB input. With all these features loaded into a single television the Bravia EX500 is an ideal choice to complement your digital lifestyle.var wcCpi\u003d\"A3477150\";",
"link": "http://lt.dell.com/lt/lt.aspx?CID\u003d27530&LID\u003d627063&DGC\u003dSS&DGSeg\u003dDHS&DURL\u003dhttp://accessories.us.dell.com/sna/productdetail.aspx?c\u003dus%26l\u003den%26s\u003ddhs%26cs\u003d19%26sku\u003dA3477150%26CAWELAID\u003d469727188",
"brand": "Sony",
"condition": "new",
"gtin": "00027242784932",
"gtins": [
"00027242784932"
],
"inventories": [
{
"channel": "online",
"availability": "inStock",
"price": 1348.0,
"currency": "USD"
}
],
"images": [
{
"link": "http://accessories.us.dell.com/sna/images/products/large/A3477150temp.jpg"
}
]
}
},
{
"kind": "shopping#product",
"id": "tag:google.com,2010:shopping/products/1113342/9429326904502109794",
"selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/9429326904502109794",
"product": {
"googleId": "9429326904502109794",
"author": {
"name": "Walmart",
"accountId": "1113342"
},
"creationTime": "2011-03-26T23:58:51.000Z",
"modificationTime": "2011-08-04T19:42:49.000Z",
"country": "US",
"language": "en",
"title": "Sony Bravia 32\" Class 3D LED-LCD 1080p 240Hz HDTV,1.68\" ultra-Slim,",
"description": "Note: You must have a source of HD programming in order to take full advantage of the Sony Bravia 32\" Class 3D 1080p LED HDTV. Contact your local cable or satellite TV provider for details on how to upgrade.Visit the Sony Brand Shop for 3D glasses, other 3D HDTVs and more.Sony Bravia 32\" Class 3D 1080p LED HDTV 240Hz, KDL-32EX720:See It All In 3D: Engineered with advanced technologies, the EX720 Series is more than a fully-loaded HDTV with 3D. Once you slide on the active shutter glasses (sold separately), the on-screen image is precisely synchronized and delivered with the Full HD image intact for an immersive entertainment viewing experience with maximum resolution and superb clarity.Slim Design, Stunning Picture: Experience the next level of picture quality and contrast with Edge LED Backlight technology. U",
"link": "http://www.walmart.com/ip/Sony-KDL-32EX720/15780230?sourceid\u003d1500000000000003142050&ci_src\u003d14110944&ci_sku\u003d15780230",
"brand": "Sony",
"condition": "new",
"gtin": "00027242817135",
"gtins": [
"00027242817135"
],
"inventories": [
{
"channel": "online",
"availability": "inStock",
"price": 898.0,
"currency": "USD"
}
],
"images": [
{
"link": "http://i.walmartimages.com/i/p/00/02/72/42/81/0002724281713_500X500.jpg"
}
]
}
}
]
}
您的JSON是数组 和对象 的混合 体 。因此,数组表示法不适用于所有项目。例如,要找到品牌用途:
foreach ($json->items as $item) {
var_dump($item->product->brand);
}
键盘示例
JSON规范
本文向大家介绍用PHP解析XML,包括了用PHP解析XML的使用技巧和注意事项,需要的朋友参考一下 XML数据提取可能是一项常见的任务,但是要直接使用此数据,您需要了解PHP如何解析XML。在PHP中解析XML涉及各种不同的功能,所有这些功能协同工作以从XML文档中提取数据。我将完成所有这些功能,并在最后将它们联系在一起。 xml_parser_create() 此函数用于创建解析器对象,该对象将
问题内容: 我已经多次解析JSON数据,但是由于某种原因,无法找到嵌套数据时要使用的正确语法。我正在尝试从此JSON解析“资产”,但是无论我尝试什么,都继续获取为foreach()提供的无效参数。 我希望这是… 问题答案: 来自php官方文档:http : //php.net/manual/fr/function.json- decode.php 第二个func arg用于assoc数组返回。如果
问题内容: 想知道为什么我的PHP代码不会在JSON数据中显示“值”的所有“值”: 第一个foreach工作正常,但第二个抛出错误。 问题答案: 您可能想要执行以下操作:
问题内容: 我想用一种特殊的方式解析一个CSS文件。 例: : 我希望PHP返回给我每个名称中包含postclass的类名。 在此示例中,结果看起来像一个数组: 但是我在正则表达式方面更糟。以某种方式搜索“ postclass”,然后抓取孔线并放入数组中。 谢谢,我用它来解析类似confic文件的css文件。 是我的最终代码。这样我就可以在不编辑布局的情况下轻松地将div包裹在一些hardcode
问题内容: 如何解析HTML / XML并从中提取信息? 问题答案: 本机XML扩展 我更喜欢使用本机XML扩展]之一,因为它们与PHP捆绑在一起,通常比所有第3方库都快,并为我提供了所需的所有标记控制权。 DOM DOM扩展使您可以使用PHP 5通过DOM API通过XML文档进行操作。它是W3C的Document Object Model Core Level 3的实现,它是一种平台和语言无关
问题内容: 我正在提取无效的JSON JSON提要。它完全缺少引号。我尝试了一些操作,例如和,以使该字符串看起来更像有效的JSON,但是在其中包含关联JSON字符串的情况下,通常会弄糟。 这是一个例子: 是否有任何php的JSON解析器可以处理这样的无效JSON? 编辑:我试图在此字符串上使用。它什么也不返回。 问题答案: 所有引号应为双引号而不是单引号。 所有键都应加引号。 整个元素应该是一个对