In this article I will write about working with JSON. JSON stands for JavaScript Object Notation. It is a great way to send objects back and forth through client-server and also to create API’s that return JSON when information is requested. Lets look at an object in javascript.
var object = { self : {name:"customer1",job:"cashier"}, todo :"jsonobject", names : [{employ:"work",happy:true,work:"program"}, {employ:"job",happy:false,work:"intro"}] };
This object, called object has a couple variables and an array names. Now lets see this object in JSON format.
{"self":{"name":"customer1","job":"cashier"}, "todo":"jsonobject", "names":[{"employ":"work","happy":true,"work":"program"}, {"employ":"job","happy":false,"work":"intro"}]}
in javascript to turn an object into JSON you use the JSON.Stringify method
JSON.stringify(object);
And to turn JSON into and Object you use JSON.Parse method
var object = JSON.parse(json);
Now to work with JSON in php you can make objects in php and then use json_encode to turn the object into JSON like this
<?php $myObj = new \stdClass(); $myObj->name = "John"; $myObj->age = 30; $myObj->city = "New York"; $myArray = array(); for($i = 0;$i < 10;$i++){ $myArray[$i] ="hello world".$i; } $myObj->hello = $myArray; $myJSON = json_encode($myObj); echo $myJSON; ?>
Now the variable $myJSON will look like this after you encode it
{"name":"John", "age":30, "city":"New York", "hello":["hello world0","hello world1", "hello world2","hello world3","hello world4", "hello world5","hello world6","hello world7", "hello world8","hello world9"]}
and if you are sending a JSON object to a php script using JSON.stringify you can grab the object in php with something like this using json-decode
$obj = json_decode($_POST["x"], false);
And that is pretty much the basics for using JSON in javascript and php. One note i was looking around and it appears that you are not supposed to send functions through JSON but there is a way that is not consistent so it shouldnt actually be done, as far as i know. I hope you enjoyed this article. Untill next time 🙂
TP-Link Deco AX3000 WiFi 6 Mesh System(Deco X55) - Covers up to 6500 Sq.Ft. , Replaces Wireless Router and Extender, 3 Gigabit ports per unit, supports Ethernet Backhaul (3-pack)
$169.98 (as of October 12, 2024 09:54 GMT -04:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire 7 tablet (newest model) 7” display, read and watch, under $80 with 10-hour battery life, 32 GB, Black
$49.99 (as of October 12, 2024 09:54 GMT -04:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire HD 10 tablet (newest model) built for relaxation, 10.1" vibrant Full HD screen, octa-core processor, 3 GB RAM, 64 GB, Ocean
$179.99 (as of October 12, 2024 09:54 GMT -04:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Apple 2022 MacBook Air Laptop with M2 chip: Built for Apple Intelligence, 13.6-inch Liquid Retina Display, 8GB RAM, 256GB SSD Storage, Backlit Keyboard, 1080p FaceTime HD Camera; Midnight
$719.04 (as of October 12, 2024 09:54 GMT -04:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire 7 Kids tablet (newest model) ages 3-7. Top-selling 7" kids tablet on Amazon | 6-months of ad-free content with parental controls included, 10-hr battery, 16 GB, Blue
$99.99 (as of October 12, 2024 09:54 GMT -04:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Author: John Rowan
I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.