Array is not converted to JSON

Asked

Viewed 788 times

1

I treat some functions via PHP and in the end return to the client via AJAX useful information. Such information is contained in a array which is converted to JSON through json_encode(). The problem is that it is sent me an error message with the following:

Syntaxerror: Unexpected end of input

This mistake happens because the json_encode() is not returning me any array. I have my variable:

$data = array(
    'command' => $command,
    'message' => $message
);

And the result of var_dump to the json_encode() is the following (I put the HTML code for better visualization, run it):

<pre class='xdebug-var-dump' dir='ltr'><small>boolean</small> <font color='#75507b'>false</font>
</pre>

The problem only happens when the variable value $message is coming from an error in the database, if I write anything in it, the return is normal:

<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'{&quot;command&quot;:2,&quot;message&quot;:&quot;Qualquer coisa&quot;}'</font> <i>(length=40)</i>
</pre>{"command":2,"message":"Qualquer coisa"}

Follows below the var_dump of an example of variable return $message:

<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'Erreur de syntaxe pr�s de &#39;&#39;NomeDeUsuario&#39;, &#39;$2a$05$Zx3hjrLOnWvNzZpRIhdcPecjreTjjaBkaYLrH7IRcfmn110et/92G&#39;,&#39; � la ligne 1'</font> <i>(length=121)</i>
</pre>

Note that this is an error in the database. The error itself is not relevant, I caused it myself to test the return of errors to the customer.

1 answer

2


By mistake Erreur de syntaxe pr�s with this character , I assume you’re trying to pass a string that uses iso-8859-1(latin) instead of utf-8, your page is likely to be utf8, but there are files included using iso-8859-1

To solve more easily, you can convert the string to utf-8 first:

$data = array(
    'command' => $command,
    'message' => $message
);

$data = utf8_encode($data);

echo json_encode($data);

Details:

Your bank may even be in utf-8, but it may be that the way you made the connection may be bringing and some files may be using ANSI with accents of the kind iso-8859-1, read this link can help you standardize your project:

  • Perfect! I would never think the problem had anything to do with it. Is there any way to "convert" the entire bank to UTF-8, so I don’t have to treat the answers that way? I tried to use ALTER DATABASE \db` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci` but the error continues with the wrong accents.

  • @Rafaelalmeida But are you sure it is the bank that is not in utf-8, or is the connection? This code ALTER DATABASE is just a situation, as I said in the link I posted, there are several things to check. For example I use utf8_unicode_ci, but other users use utf8_general_ci

  • I just recreated the table: http://i.imgur.com/zm8oUPk.png. But the accents are still wrong. What do you mean by "connection"? I added a header UTF-8 in the PHP class that returns the data and still nothing.

  • @Rafaelalmeida you used $mysqli->set_charset('utf8') or with PDO: $conn->exec('SET CHARACTER SET utf8');? Like I said, buddy, it’s not just doing one thing or another, you have to project following every step.

  • Lack of attention, I didn’t see the Dit you made. Thank you very much, I was able to convert everything. :)

  • @Rafaelalmeida Success for you!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.