无效的PHP JSON编码 [英] Invalid PHP JSON encoding

查看:85
本文介绍了无效的PHP JSON编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP(5.3.1)中的一个项目上,我需要将JSON字符串发送到Web服务(在python中),但是我从json_encode获得的结果未作为有效的JSON传递(i' m使用JSLint来检查有效性).

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).

我应该补充一点,我要编码的结构相当大(13K编码),并且部分包含UTF8数据,尽管json_encode确实可以处理它,但结果中却出现了空格.例如,我可以得到{"hello":tru e}或{"hell o":true},由于JSON无效(或数据,如第二个示例),这会导致web服务出现错误.

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).

我还尝试过使用Zend框架进行JSON编码,但这并没有太大不同.

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

PHP中的JSON是否存在已知问题?有没有人遇到这种行为并找到了解决方案?

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

推荐答案

前几天,我在处理一些自动生成的电子邮件,并注意到相同的怪异行为(在电子邮件正文中插入了空格),所以我开始检查电子邮件发布并找到罪魁祸首:

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:

从SMTP RFC2821:

From the SMTP RFC2821:

文本的最大总长度 包括的行是1000 字符(不计首 点重复以提高透明度).

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

我的电子邮件正文确实在一行中,因此用\ n打破它可以解决空格问题.

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

这篇关于无效的PHP JSON编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆