Can't seem to find the solution to this, I want to load json data from a view variable like this
$this->json = $this->load->view('/jsonfiles/page.json','',true);
Outputting
var_dump(json_decode($this->json,true));
= NULL
The View seems to be rendering the .json file incorrectly, for the json_decode to fail. (maybe not incorrectly, but in a different format), Ive already tried to utf8_encode, no luck
JSON Variable within the page.json
[
{
"page" : {
"name" : "PageNameHere",
"date" : true
}
}
]
Or is there an alternative to doing this?
UPDATE: Yes it works when its an actual string in the variable like this
$this->json = '[{"page":{"name":"PageNameHere","date":true}}]';
var_dump(json_decode($this->json,true));
SOLUTION: So I've decided to figure this out one, and by my mistake, I had a "," comma in the last part of my JSON, which wasn't in this thread, I shorten the JSON for topic, but I found it, using this neat error switch, able to find JSON rendering errors
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
My Issue was labeled as "Syntax error, malformed JSON"
$this->load->view()should be in JSON format? The CodeIgniter docs, simply says "...returns data as a string rather than sending it to your browser.". It says nothing about the format of this string.json_decode()only takes a JSON encoded string as it's input. If$this->load->view()is not giving you a JSON encoded string, then not sure what you expect.