From: Dave M G on
PHP Users,

I'm decoding some JSON data in PHP to convert it into an array.

However, it's not working, and json_last_error() is returning a value of
"4", which I believe means "Malformed UTF-8 characters, possibly
incorrectly encoded".

I try at every turn in every setting to ensure that all my code and
connections are in UTF-8.

But how can I be sure it's valid UTF-8? I've tried using the PHP command
utf8_encode() on the string, but that hasn't changed anything.

And can I trust the error message?

Any help or advice would be much appreciated.

By the way, here is the code I'm currently testing with. I'm just
encoding and then decoding a string right in the PHP just to get it to
work before I even try getting the data from anywhere else.

$myData ='{"display_name":"Test
Guy","email":"testguy(a)testaddress.com","timeout":"1279145273"}';
$myArray1 = json_encode($myData);
$myArray2 = utf8_encode (stripslashes($myArray1));
$myArray = json_decode($myArray2, true);
$jsonerror = json_last_error();

--
Dave M G
From: Jim Lucas on
Dave M G wrote:
> PHP Users,
>
> I'm decoding some JSON data in PHP to convert it into an array.
>
> However, it's not working, and json_last_error() is returning a value of
> "4", which I believe means "Malformed UTF-8 characters, possibly
> incorrectly encoded".
>
> I try at every turn in every setting to ensure that all my code and
> connections are in UTF-8.
>
> But how can I be sure it's valid UTF-8? I've tried using the PHP command
> utf8_encode() on the string, but that hasn't changed anything.
>
> And can I trust the error message?
>
> Any help or advice would be much appreciated.
>
> By the way, here is the code I'm currently testing with. I'm just
> encoding and then decoding a string right in the PHP just to get it to
> work before I even try getting the data from anywhere else.
>
> $myData ='{"display_name":"Test
> Guy","email":"testguy(a)testaddress.com","timeout":"1279145273"}';
> $myArray1 = json_encode($myData);
> $myArray2 = utf8_encode (stripslashes($myArray1));
> $myArray = json_decode($myArray2, true);
> $jsonerror = json_last_error();
>

Were you meaning to strip the slashes?? That will break things... They are
their for a reason. :)

Try it without that stripslashes() and see what happens.

Also, if on that error page they are using a bitwise numbering scheme it would
not be the last one, it would be the third error message

Value Constant
1 JSON_ERROR_NONE
2 JSON_ERROR_DEPTH
4 JSON_ERROR_CTRL_CHAR
8 JSON_ERROR_SYNTAX
16 JSON_ERROR_UTF8

Just to be sure, echo each of the above constants and see what the value is.

And, if my suspicion is correct it is going to be because when you stripslashes
and then try and decode it, it breaks because the escaped characters are not
longer escaped.

My suggestion would be to UTF*_encode() each piece of data before you stuff it
into your json string. Then one you have built your json string from the
encoded data, run it through json_encode() and you should be fine.

2 pennies for my thoughts please... :)

--
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?