JSON
To convert an array to a string, you can use the functions in packages\base\json. There are two functions: one converts an array to a string, and the other converts a JSON string to an array.
You can safely use these functions for arrays or Persian-language strings without any concern about character garbling.
Converting an Array to a String
To convert an array to a string (for various purposes, such as storing it in a database), you should use the encode function. This function accepts a value of any type as its first argument, and as its second argument it accepts the json_encode constants defined in PHP. (The second argument is optional.) The output of this function is a string.
json\encode($input, $option);
Example
<?php
namespace packages\packagename\controllers;
use packages\base\{Controller, Json};
class Test extends Controller {
public function encodeArray(){
$array = array("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5);
try {
echo json\encode($array);
echo json\encode($array, JSON_PRETTY_PRINT);
/* output
{"a":1,"b":2,"c":3,"d":4,"e":5}
{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5
}
*/
}catch(Json\JsonException $e) {
echo "JsonException: {$e->getMessage()}";
}
}
}
Converting a JSON String to an Array
To convert a string (which must be a JSON string) into an array, you should use the decode function. This function accepts a string as its first parameter. If you set this function's second value to true, the output is an array; otherwise, it is an object.
json\decode(string);
Example
<?php
namespace packages\packagename\controllers;
use packages\base\{Controller, Json};
class Test extends Controller {
public function decodeJson(){
$json = '{"a":1, "b":2, "c":3, "d":4, "e":5}';
try {
var_dump(json\decode($json));
var_dump(json\decode($json, true));
/* output
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
*/
}catch(Json\JsonException $e) {
echo "JsonException: {$e->getMessage()}";
}
}
}
The JsonException Exception
When working with the Json class, the data encoding/decoding operation may not complete correctly (the input may be wrong, or occasionally the process may not proceed correctly) and may cause an error; in that case, an exception of type packages\base\json\JsonException is thrown.
Calling the getMessage() method on the JsonException object returns the cause of the error that occurred.
Example
<?php
namespace packages\packagename\controllers;
use packages\base\{Controller, Json};
use packages\packagename\Question as Model;
class Questions extends Controller {
public function insert(){
$inputRules = [
'question' => [
'type' => 'string'
],
'options' => []
];
$this->response->setStatus(false);
$inputs = $this->checkinputs($inputRules);
$options = [];
try {
foreach($inputs['options'] as $key => $row){
$options[] = [
'label' => $row,
'value' => $key
];
}
$options = Json\encode($options);
$question = new Model();
$question->question = $inputs['question'];
$question->options = $options;
$question->save();
$this->response->setStatus(true);
}catch(Json\JsonException $e) {
echo "JsonException: {$e->getMessage()}";
}
return $this->response;
}
}