以下是我的应用程序从邮戳收到的JSON字符串,即邮件收件服务:
{
"From": "me@mydomain.com",
"FromFull": {
"Email": "me@mydomain.com",
"Name": "Jack"
},
"To": "\"test@email.mydomain.com\" <test@email.mydomain.com>",
"ToFull": [
{
"Email": "test@email.mydomain.com",
"Name": "test@email.mydomain.com"
}
],
"Cc": "",
"CcFull": [],
"ReplyTo": "",
"Subject": "Brussel Sprouts",
"MessageID": "a97fb074-338e-48c5-97db-d9c5155e9307",
"Date": "Sun, 30 Dec 2012 18:10:54 +0000",
"MailboxHash": "",
"TextBody": "Are Nice",
"HtmlBody": "",
"Tag": "",
"Headers": [
{
"Name": "X-Spam-Checker-Version",
"Value": "SpamAssassin 3.3.1 (2010-03-16) onrs-iad-pm-inbound1.wildbit.com"
},
{
"Name": "X-Spam-Status",
"Value": "No"
},
{
"Name": "X-Spam-Score",
"Value": "0.0"
},
{
"Name": "X-Spam-Tests",
"Value": "RCVD_IN_DNSWL_BLOCKED"
},
{
"Name": "Received-SPF",
"Value": "None (no SPF record) identity=mailfrom; client-ip=209.85.212.175; helo=mail-wi0-f175.google.com; envelope-from=me@mydomain.com; receiver=test@email.mydomain.com.com"
},
{
"Name": "X-Google-DKIM-Signature",
"Value": "v=1; a=rsa-sha256; c=relaxed\/relaxed; d=google.com; s=20120113; h=x-received:from:content-type:content-transfer-encoding:subject :message-id:date:to:mime-version:x-mailer:x-gm-message-state; bh=JrLpPQqTnp0QixED2qZ5+zepXQXuPLJWqihFECt8uJ4=; b=EJHac6BaGi3laxnMZZ3Lf\/ervtavKTG0+DHXRRCfGH2HSG42wN\/qIj6IQ0G36NFsjT x2tnYRhj7aBRseky+YEyAF99kUOx\/p8qaCWo7wN1cf3dTJUrrQu\/vrdgdXezfheKW49z x4\/d\/8f2bgJN5MvTtZj15WmqZRCDt8\/QJVZvP7J5hANdxqULgwAJCaCJFU3CTNu66nT+ M1tPSDfZwTdNsS70Pna75Y3bRtlJPxXF6gesYhXK\/cnDbqsCopcAiEc9guMVMnqAjIRu XqBWLft4Bom5H2ViNj5V\/A3GCmP44h\/OM1h+h2zBTYJ3fJK4zyluok8K5GIjn9KN1o2w v4eA=="
},
{
"Name": "X-Received",
"Value": "by 10.180.72.146 with SMTP id d18mr51711599wiv.33.1356891056971; Sun, 30 Dec 2012 10:10:56 -0800 (PST)"
},
{
"Name": "Content-Transfer-Encoding",
"Value": "7bit"
},
{
"Name": "Message-Id",
"Value": "<224B85F9-B9C2-4273-A81C-7C340E8A66B2@mydomain.com>"
},
{
"Name": "Mime-Version",
"Value": "1.0 (Mac OS X Mail 6.2 \\(1499\\))"
},
{
"Name": "X-Mailer",
"Value": "Apple Mail (2.1499)"
},
{
"Name": "X-Gm-Message-State",
"Value": "ALoCoQl0eQmrBNRNbe7T06oTeDXcEA3LDLJurR2U0Yj64EyOA2Iy3YL6CyiTodGXtuJHIuMlm5a1"
}
],
"Attachments": []
}
将其复制到任何JSON Validator中,您将看到它是有效的。
但是,当我在PHP中对此字符串运行json_decode();
时,它会无声地失败。运行echo json_last_error();
时,返回的错误代码为4
。这对应于语法错误。
在我的代码编辑器中简单地将\
替换为\\
,我可以让它工作,但我怎样才能让PHP自动执行此操作?
编辑:正如在评论中发现的那样,上面发布的确切字符串在从文件读入时正常工作,因为PHP不会尝试解析任何反斜杠等。但是,它似乎是从file_get_contents('php://input)
读入时解析斜杠,这正是导致我出现问题的原因。
答案 0 :(得分:5)
您可以对变量执行此操作:
$json = str_replace('\\', '\\\\', $json);