How to Handle JSON data using PHP

Since JSON appeared on the web, all web services are offering JSON as their primary data format for data interchanging. The reason JSON is so popular is because it is lightweight and easy for humans to read and write. Today I want to show you how we can use this JSON data in our projects using PHP. Let's say a health website is offering information about fruits in JSON data to the public, and you want to use them on your web project. Their JSON data looks like this :
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
{ "Apple":"Helps lower the chance of developing diabetes and asthma.", "Avocado":"Helps lower cholesterol levels when eaten instead of harmful saturated fats.", "Banana":" 105 calories, 3 g fibre, source of vitamin B6, potassium and folate", "Blackberry":"Helps reduce the risk of stroke and cancer." }
Using PHP we can easily fetch and decode this information :
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
//get JSON data using either file_get_contents or cURL $result = file_get_contents("http://some-health-site.com/path/to/json/"); //decode JSON string & convert it into a PHP variable. $json_object = json_decode($result); //simply print-out the variable echo $json_object->Avocado; echo $json_object->Banana;
  • I used this example this way(my previous post was incomplete), Banana; echo $json_object->Banana; ?> --------------------------------- And in process.php i have { “Apple”:”Helps lower the chance of developing diabetes and asthma.”, “Avocado”:”Helps lower cholesterol levels when eaten instead of harmful saturated fats.”, “Banana”:” 105 calories, 3 g fibre, source of vitamin B6, potassium and folate”, “Blackberry”:”Helps reduce the risk of stroke and cancer.” } But when i run the php code i get this : Notice: Trying to get property of non-object in C:\wamp\www\trial\afr.php on line 54 Notice: Trying to get property of non-object in C:\wamp\www\trial\afr.php on line 55 =================================== Please i’ll need urgent assistance in troubleshooting this because i need it for a job interview, thanks.
  • I used this example this way, Avocado; echo $json_object->Banana; ?> And in process.php i have { "Apple":"Helps lower the chance of developing diabetes and asthma.", "Avocado":"Helps lower cholesterol levels when eaten instead of harmful saturated fats.", "Banana":" 105 calories, 3 g fibre, source of vitamin B6, potassium and folate", "Blackberry":"Helps reduce the risk of stroke and cancer." } But when i run the php code i get this : Notice: Trying to get property of non-object in C:\wamp\www\trial\afr.php on line 54 Notice: Trying to get property of non-object in C:\wamp\www\trial\afr.php on line 55 Please i'll need urgent assistance in troubleshooting this because i need it for a job interview, thanks.
  • Hello what if yo have a json file like this how to you loop it: { "status": "ok", "ticket": { "id": 2007, "number": 2, "client": { "id": 5700, "avatar": null, "name": "John Smith", "nippesel": "36041916912", "address": "19 Baker st., SEATTLE WA 98104", "removed": false, "correspondenceAddress": "", "notes": "", "vip": false, "ticketsTotalNew": 0, "ticketsTotalOpen": 1, "ticketsTotal": 1,"contacts": [ { "id": 1293, "type": 1, "value": "[email protected]" }, { "id": 1296, "type": 2, "value": "88 596 13 33" } ], "contactsPersons": [], "guards": [], "stars": [] } }, "client_id": 5700, "visited": true, "added": { "date": "2014-10-02 13:47:35", "timezone_type": 3, "timezone": "UTC" }, "closed": null, "state": 0, "numberOfAnswers": 1, "title": "Example ticket", "unreaded": false, "answers": [ { "id": 3984, "content": "Example ticket content", "private": false, "added": { "date": "2014-10-02 13:47:35", "timezone_type": 3, "timezone": "UTC" }, "newState": null, "user_id": null, "seconded": [] } ], "stars": [], "assignedUsers": [ 843 ], "dateLastAnswer": { "date": "2014-10-02 13:47:35", "timezone_type": 3, "timezone": "UTC" }, "reportedBy": 1, "activity": {}, "urgent": false }
New question is currently disabled!