SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Frank YuFrank Yu Posts: 22Questions: 7Answers: 3

The following is the json data I got from my server. However, I got the error msg as titled. But if I retype the first 8 letters ( {"data": ) , the data could be passed the testing of the http://json.parser.online.fr/ . Anyone has any idea why?

{"data":[{"guid":"0001-c0a801da-526a34b8-2b49-5a8e79e8","cs":{"corpid":"V","username":"Frank","newpassword":"","cname":"\u4f59\u826f\u5bec","ename":"Frank","phone":null,"email":"frank.yu@veden.dental","isadmin":"Y","isgroupa":"Y","groupaguid":"0001-7f000001-54c7999a-9d94-71ee1c39","isgroupc":"Y","groupcguid":"0001-7f000001-54c7999a-9d94-71ee1c39","enddate":null,"lastlogin":"2015-09-01 15:24:59","memo":null,"corpcname":"\u7dad\u767b","groupacname":"\u7cfb\u7d71\u7ba1\u7406\u54e1","groupccname":"\u7cfb\u7d71\u7ba1\u7406\u54e1"}}],"options":""}

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,696Questions: 1Answers: 10,500 Site admin

    Can you please link to a debug trace or the page showing the issue.

    Allan

  • Frank YuFrank Yu Posts: 22Questions: 7Answers: 3

    It's very weired that if you copy the data from {...} to the sublime and put the cursor at the beginning and press Delete key, you will find that there is "space" before the {. That means you have to press the Delete key twice to delete the {.

  • allanallan Posts: 63,696Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Sounds like you might have a BOM prior to the JSON data. Might be worth searching for and removing that.

    Allan

  • Frank YuFrank Yu Posts: 22Questions: 7Answers: 3
    edited September 2015 Answer ✓

    Thanks Allan for the advice. Finally I use the following codes to check and remove the BOM from all of the php files and got my system working.

    <?php
    //此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除
    //By Bob Shen
     
    $basedir = ".";    //修改此行为需要检测的目录,点表示当前目录
    $auto = 1;    //是否自动移除发现的BOM信息。1为是,0为否。
     
    // 以下不用改动
    if ($dh = opendir($basedir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file")." <br>";
        }
        closedir($dh);
    }
     
    function checkBOM ($filename) {
        global $auto;
        $contents = file_get_contents($filename);
        $charset[1] = substr($contents, 0, 1);
        $charset[2] = substr($contents, 1, 1);
        $charset[3] = substr($contents, 2, 1);
        if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
            if ($auto == 1) {
                $rest = substr($contents, 3);
                rewrite($filename, $rest);
                return ("<font color=red>BOM found, automatically removed.</font>");
            } else {
                return ("<font color=red>BOM found.</font>");
            }
        }
        else return ("BOM Not Found.");
    }
     
    function rewrite ($filename, $data) {
        $filenum = fopen($filename, "w");
        flock($filenum, LOCK_EX);
        fwrite($filenum,$data);
        fclose($filenum);
    }
    
    
  • allanallan Posts: 63,696Questions: 1Answers: 10,500 Site admin

    Good to hear. Thanks for posting your solution.

    Allan

This discussion has been closed.