Php parse json files

Parsing JSON with PHP

I’m attempting to pull in data from Google’s Shopping API. I’m able to download the data successfully, but I’m having trouble parsing through it with PHP. I’m still learning, but I seem to be having issues with multi-dimensional arrays. I capture the JSON with $json = json_decode($data); . The following just echos the outer array, but I can’t pull from the inner array:

foreach($json as $key => $value)

If I want to grab the «title», «description», «brand», and «availability» for each product, how would I parse through it?

< "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" >] > > ] > 

Источник

Читайте также:  Javascript function this that

Parse a JSON File in PHP

Parse a JSON File in PHP

In this article, we will introduce the method to parse a JSON file in PHP.

The contents of the JSON file used in the example codes are as follows.

[    "id": "01",  "name": "Olivia Mason",  "designation": "System Architect"  >,    "id": "02",  "name": "Jennifer Laurence",  "designation": "Senior Programmer"  >,    "id": "03",  "name": "Medona Oliver",  "designation": "Office Manager"  > ] 

Use file_get_contents() Function to Parse a JSON File in PHP

The built-in function file_get_contents() is used to read a file and store it into a string. By using this function, we can parse a JSON file to a string. The correct syntax to use this function is as follows.

file_get_contents($pathOfFile, $searchPath, $customContext, $startingPoint, $length); 

The following program shows how to parse a JSON file.

php $JsonParser = file_get_contents("myfile.json"); var_dump($JsonParser); ?> 

The function file_get_contents() have only parsed the JSON data stored in a JSON file. We cannot use this data directly.

string(328) "[    "id": "01",  "name": "Olivia Mason",  "designation": "System Architect"  >,    "id": "02",  "name": "Jennifer Laurence",  "designation": "Senior Programmer"  >,    "id": "03",  "name": "Medona Oliver",  "designation": "Office Manager"  > ]" 

To make this data useful, we can use json_decode() to convert the JSON string to an array. Using this function in the below program.

php $Json = file_get_contents("myfile.json"); // Converts to an array  $myarray = json_decode($Json, true); var_dump($myarray); // prints array  ?> 
array(3)   [0]=>  array(3)   ["id"]=>  string(2) "01"  ["name"]=>  string(12) "Olivia Mason"  ["designation"]=>  string(16) "System Architect"  >  [1]=>  array(3)   ["id"]=>  string(2) "02"  ["name"]=>  string(17) "Jennifer Laurence"  ["designation"]=>  string(17) "Senior Programmer"  >  [2]=>  array(3)   ["id"]=>  string(2) "03"  ["name"]=>  string(13) "Medona Oliver"  ["designation"]=>  string(14) "Office Manager"  > > 

Related Article — PHP JSON

Copyright © 2023. All right reserved

Источник

How to Parse JSON in PHP

Monty Shokeen

Monty Shokeen Last updated May 31, 2021

JSON, short for JavaScript Object Notation, is a common lightweight format for storing and exchanging information. As the name suggests, it was initially derived from JavaScript, but it is a language-independent format for storing information. A lot of languages like PHP now implement functions to read and create JSON data.

This tutorial will teach you how to read a JSON file and convert it to an array in PHP. Learn how to parse JSON using the json_decode() and json_encode() functions.

Reading JSON From a File or String in PHP

Let’s say you have a file which contains information in JSON format. How do you access and store it in PHP?

First, you need to get the data from the file into a variable by using file_get_contents() . Once the data is in a string, you can call the json_decode() function to extract information from the string. Keep in mind that JSON simply provides a way to store information as a string using a set of predefined rules. It is our job to decode the strings properly and get the information we want.

The json_decode() function accepts four parameters, but you will only need the first two in most situations. The first parameter specifies the string that you want to decode. The second parameter determines how the decoded data is returned. Setting it to true will return an associative array, and false will return objects. Here is a basic example. We have a file called people.json with the following contents:

Источник

Оцените статью