2

I have an Android app, PHP page and MySql db.
The android app send a string parameter to the server, I use Gson so I know that the Json string is correct.
On server side I store that string of json as Text field.
On web client I take that field from my database and doing json_decode and receives a NULL.
Most of the site written by PHPMAKER 10 so I use it's database connection.

$result="";
$rs->MoveFirst();
if ($rs) {
    $result = $rs->fields[0];
}
$rs->Close();
$extras = json_decode($result, true);

I checked that json in Json validator and it looks fine.

Any ideas why I gets null? (maybe encoding)

EDIT: I did the following check:

echo "<script> var x = {$result}; console.log(x.length)</script>";

Chrome gave the following error:

Uncaught SyntaxError: Unexpected token ILLEGAL 

EDIT 2: If I print that string echo $result and copy that to a variable it works.

11
  • 1
    What's the result of var_export($result)? Commented Mar 31, 2014 at 14:52
  • A string, I checked it with var_dump. Commented Mar 31, 2014 at 14:54
  • 1
    The json_last_error_msg function will give you to the error message. Commented Mar 31, 2014 at 14:55
  • The error is just number 4 (JSON_ERROR_SYNTAX), but I already checked the json. Commented Mar 31, 2014 at 15:00
  • Is the JSON string too long to check by hand? Start removing bits, or start by adding bits to {}, see which part the parser doesn't like. Commented Mar 31, 2014 at 15:07

2 Answers 2

3

That happened to me, when there was a UTF-8 BOM at the beginning of the JSON string. JSON is UTF-8 as default, so that BOM seems to be forbidden.

You could use this function to remove it:

//Remove UTF8 Bom

function remove_utf8_bom($text)
{
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}

Taken from here: How to remove multiple UTF-8 BOM sequences before "<!DOCTYPE>"?

Sign up to request clarification or add additional context in comments.

Comments

1

I soled the problem:
My json contained breaklines '/r' and '/r/n'.
After removing them everything was fine.

Comments

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.