Invalid PHP JSON encoding

2019-03-31 01:01发布

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

I should add that the structure I'm trying to encode is fairly big (13K encoded), and consists partially of UTF8 data, and while json_encode does handle it, i get spaces in weird places in the result. For example, I could get {"hello":tru e} or {"hell o":true} which results in an error from the webservice since the JSON is invalid (or data, like in the second example).

I've also tried to use Zend framework for JSON encoding, but that didn't make much different.

Is there a known issue with JSON in PHP? Did anyone encounter that behavior and found a solution?

4条回答
干净又极端
2楼-- · 2019-03-31 01:34

You state that "the structure I'm trying to encode ... consists partially of UTF8 data." This implies that it is also partially of non-UTF8 data. The json_encode doc has a comment at the bottom, that

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

json_encode(array('àü'));

will produce a json representation of an empty string, while

json_encode(array(utf8_encode('àü')));

will work.

Are the failing segments of the JSON due to non-UTF8 input?

查看更多
仙女界的扛把子
3楼-- · 2019-03-31 01:43

After scratching my head for nearly a day, I've come to the conclusion that the problem was not in the json_encode function. It was with my post function.

Basically, the json_encode was preparing the data to be sent to another service. Before today, I've used stream_context_create and fopen to post data to the external service, but now I use fsockopen and fputs and it seems to be working.

Although I'm unsure as to the nature of the problem, I'm happy it works now :)

BTW: After this process, I mail myself the input and output (both in JSON) and this is how I saw there was a problem in the first place. This problem still persists but I guess that's related to the encoding of the mail or something of that sort.

查看更多
祖国的老花朵
4楼-- · 2019-03-31 01:44

For sure object keys cannot contain spaces or any non unicode characters, unquoted variables can be only boolean, integer ,float, object and array value, strings should always be quoted.

Also, I would recommend you to add correct header before your json output.

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

Can you also post your array or object that you passing to json_encode?

查看更多
Deceive 欺骗
5楼-- · 2019-03-31 01:45

I was handling some automatically generated emails the other day and noticed the same weird behavior (spaces were inserted to the email body), so I started to check the email post and found the culprit:

From the SMTP RFC2821:

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

My email body was indeed in one line, so breaking it with \n's fixed the spaces issue.

查看更多
登录 后发表回答