Invalid PHP JSON Encoding

I am working on a project in PHP (5.3.1) where I need to send a JSON string to webservice (in python), but the result that I get from json_encode does not pass as valid JSON (i'm using JSLint to check for validity) .

I have to add that the structure I'm trying to encode is quite large (13 Kbytes) and partly consists of UTF8 data, and while json_encode does this, I get spaces in strange places as a result. For example, I could get {"hello": tru e} or {"hell o": true}, which leads to an error from the web service, because JSON is invalid (or data, as in the second example).

I also tried using the Zend framework to encode JSON, but that didn't differ much.

Is there a known problem with JSON in PHP? Has anyone come across this behavior and found a solution?

+4
source share
4 answers

I was processing some automatically generated emails the other day and noticed the same strange behavior (spaces were inserted into the body of the email), so I started checking the mail and found the culprit:

From SMTP RFC2821:

The maximum total text length including 1000 characters (not including the leading dot is duplicated for transparency).

My mailbox really was on the same line, so breaking it into \ n fixed the problem with spaces.

+3
source

Of course, object keys cannot contain spaces or characters of uncoded code, unmentioned variables can only be logical, integer, float, objects and array value, strings should always be specified.

Also, I would recommend that you add the correct header before json exits.

if(!headers_sent()) header('Content-Type: application/json; charset=utf-8', true,200); 

Can you also place your array or object that you pass to json_encode ?

+6
source

You state that "the structure I'm trying to code ... consists in part of UTF8 data." This means that it also partially relates to data not related to UTF8. In json_encode doc there is a comment below that

json_encode () expects strings to be encoded in UTF8 format, while by default PHP strings are encoded in ISO-8859-1. It means that

json_encode (array ('AU'));

will create a json representation of the empty string, and

json_encode (array (utf8_encode ('AU')));

will work.

Are bad JSON segments due to non-UTF8 input?

+6
source

Scratching my head for almost the whole day, I came to the conclusion that the problem is not in the json_encode function. This was with my post function.

Basically, json_encode was preparing data to send to another service. Until today, I used stream_context_create and fopen to send data to an external service, but now I use fsockopen and fputs and it seems to work.

Although I'm not sure about the essence of the problem, I'm glad that it works now :)

BTW: after this process, I send myself input and output (both in JSON) and in the way I saw that the problem was in the first place. This problem still persists, but I assume it is due to the encoding of mail or something like that.

+1
source

Source: https://habr.com/ru/post/1238950/


All Articles