Jsons section
encodeJson
decodeJson
validateJson
beautifyJson
formatJson
paginateToJsonSecure
require_once 'Jsons.php';
$jsons = new Jsons();
Encodes a PHP variable to JSON format.
$data = ['name' => 'John', 'age' => 30];
$encodedJson = $jsons->encodeJson($data);
echo $encodedJson; // Output: {"name":"John","age":30}
Decodes a JSON string into a PHP variable.
$jsonString = '{"name":"Jane","age":25}';
$decodedData = $jsons->decodeJson($jsonString);
print_r($decodedData); // Output: Array([name] => Jane, [age] => 25)
Validates the structure of a JSON string.
$isJsonValid = $jsonHandler->validateJson($encodedJson);
echo "Is JSON valid? " . ($isJsonValid ? 'Yes' : 'No'); // Is JSON valid? Yes
Beautifies a JSON string for readability.
$uglyJson = '{"key1":"value1","key2":"value2"}';
$beautifiedJson = $jsons->beautifyJson($uglyJson);
echo $beautifiedJson; // Output:
/*
{
"key1": "value1",
"key2": "value2"
}
*/
Formats a JSON string into a well-presented, human-readable format.
$jsonString = '{"name":"Alice","age":35}';
$formattedJson = $jsons->formatJson($jsonString);
echo $formattedJson; // Output:
/*
{
"name": "Alice",
"age": 35
}
*/
Paginates an array of data and returns a secure and comprehensive JSON response with pagination details.
$data = ['item1', 'item2', 'item3', 'item4', 'item5'];
$currentPage = 2;
$perPage = 2;
$baseUrl = 'https://example.com/data';
$responseData = ['message' => 'Pagination example'];
$paginatedJson = $jsons->paginateToJsonSecure($data, $currentPage, $perPage, $baseUrl, $responseData);
echo $paginatedJson; // Output: JSON string with paginated data and pagination details