Created
April 6, 2017 05:23
-
-
Save bablukpik/6de4fa603af36b149533b9aed74ca5d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//JSON is a light-weight, language independent, data interchange format. | |
-> JSON Encode and Decode | |
-> JSON Encode: | |
->PHP json_encode() function is used for encoding JSON in PHP. Objects in PHP can be converted into JSON by using the PHP function json_encode(). | |
i.e, | |
$myObj->name = "John"; | |
$myObj->age = 30; | |
$myObj->city = "New York"; | |
$myJSON = json_encode($myObj); | |
echo $myJSON; | |
->JSON Decode: | |
-> json_decode, that decodes a JSON string into an object. The second parameter accepts a boolean that when set as true, tells it to return the objects as associative arrays. | |
i.e, $obj = json_decode($_GET["x"], false) | |
<?php | |
For example: | |
// JSON string | |
$someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]'; | |
// Convert JSON string to Array | |
$someArray = json_decode($someJSON, true); | |
print_r($someArray); // Dump all data of the Array | |
echo $someArray[0]["name"]; // Access Array data | |
// Convert JSON string to Object | |
$someObject = json_decode($someJSON); | |
print_r($someObject); // Dump all data of the Object | |
echo $someObject[0]->name; // Access Object data | |
?> | |
//Loop through PHP Array or Object | |
<?php | |
->$value means arrays or objects if multidimensional array | |
// Loop through Array | |
$someArray = ...; // Replace ... with your PHP Array | |
foreach ($someArray as $key => $value) { | |
echo $value["name"] . ", " . $value["gender"] . "<br>"; | |
} | |
// Loop through Object | |
$someObject = ...; // Replace ... with your PHP Object | |
foreach($someObject as $key => $value) { | |
echo $value->name . ", " . $value->gender . "<br>"; | |
} | |
?> | |
-> Convert PHP Array or Object to JSON String | |
<?php | |
// Array | |
$someArray = [ | |
[ | |
"name" => "Jonathan Suh", | |
"gender" => "male" | |
], | |
[ | |
"name" => "William Philbin", | |
"gender" => "male" | |
], | |
[ | |
"name" => "Allison McKinnery", | |
"gender" => "female" | |
] | |
]; | |
// Convert Array to JSON String | |
$someJSON = json_encode($someArray); | |
echo $someJSON; | |
?> | |
-> Featured in PHP 5.4+ | |
<?php | |
$array = array( | |
"foo" => "bar", | |
"bar" => "foo" | |
); | |
// as of PHP 5.4 | |
$array = [ | |
"foo" => "bar", | |
"bar" => "foo" | |
]; | |
?> | |
//Convert JSON String to JavaScript Object | |
-> JSON Decode in Jabascript: JavaScript has a built-in JSON.parse() method that parses a JSON string and returns an object. | |
<script> | |
// Convert JSON String to JavaScript Object | |
var JSONString = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]'; | |
var JSONObject = JSON.parse(JSONString); | |
console.log(JSONObject); // Dump all data of the Object in the console | |
alert(JSONObject[0]["name"]); // Access Object data | |
</script> | |
->JSON.parse() is very well-supported, but there are browsers that do not support it (i.e. <= IE 7). Hence we can use jQuery method. | |
<script> | |
// Convert JSON String to JavaScript Object with jQuery | |
var JSONString = "..."; // Replace ... with your JSON String | |
var JSONObject = $.parseJSON(JSONString); | |
console.log(JSONObject); // Dump all data of the Object in the console | |
alert(JSONObject[0]["name"]); // Access Object data | |
</script> | |
//Loop through JavaScript Object | |
<script> | |
// Loop through Object | |
var JSONObject = ...; // Replace ... with your JavaScript Object | |
for (var key in JSONObject) { | |
if (JSONObject.hasOwnProperty(key)) { | |
console.log(JSONObject[key]["name"] + ", " + JSONObject[key]["gender"]); | |
} | |
} | |
</script> | |
//Or in Jquery loop through | |
var data = [ | |
{"Id": 10004, "PageName": "club"}, | |
{"Id": 10040, "PageName": "qaz"}, | |
{"Id": 10059, "PageName": "jjjjjjj"} | |
]; | |
$.each(data, function(i, item) { | |
alert(data[i].PageName); | |
}); | |
$.each(data, function(i, item) { | |
alert(item.PageName); | |
}); | |
//Or | |
var myJSON = '[{"host":"foo","url":"bar"},{"host":"foos","url":"bars"}]'; | |
$.each($.parseJSON(myJSON), function(key,value){ | |
alert(value.url); | |
}); | |
->JSON Encoding in JS: JavaScript has a JSON.stringify method to convert a value into a JSON string. | |
<script> | |
var JSONObject = [ | |
{ | |
"name": "Jonathan Suh", | |
"gender": "male" | |
}, | |
{ | |
"name": "William Philbin", | |
"gender": "male" | |
}, | |
{ | |
"name": "Allison McKinnery", | |
"gender": "female" | |
} | |
]; | |
var JSONString = JSON.stringify(JSONObject); | |
alert(JSONString); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment