0

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"

7
  • Why do you think $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. Commented Feb 16, 2015 at 18:39
  • @Sparky no, I dont want view() to be json format, I'm using view() to return my file into a string variable, however the json_decode isnt able to read the format, for whatever the damn reason is, need to look this up some more. Commented Feb 16, 2015 at 19:03
  • Well, 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. Commented Feb 16, 2015 at 19:06
  • I've tried this, var_dump(json_decode(file_get_contents('/path/to/page.json'),true)); and it still does not work, thats reading a json file directly. What am I missing? Commented Feb 16, 2015 at 19:08
  • @Sparky, you can put json encoded urls into a string, and decode them, how do I convert a string already in json format encoded for json decode? Commented Feb 16, 2015 at 19:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.