JSON无效(JSONview),但我看不到 [英] JSON is invalid (JSONview), but I don't see how

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

问题描述

好吧,我对此一直疯了.我正在从PHP输出JSON,而Chrome和Firefox的JSON View扩展都声称它无效.两种扩展都可以在 JSON View的示例上正常工作,因此看来实际上我的JSON出了点问题–但我不知道是什么.

OK, I've been going nuts with this. I'm outputting a JSON from PHP, and the JSON View extension, for both Chrome and Firefox, is claiming it's invalid. Both extensions work correctly on JSON View's example, so it seems likely that there actually is something wrong with my JSON – but I have no idea what.

Firefox版本显示错误消息:

The Firefox version has an error message:

There was an error parsing the JSON document. The document may not be well-formed.

Chrome版本缺少此类错误消息,但仍将JSON打印为纯文本.

The Chrome version lacks such error messages, but still prints the JSON as plaintext.

am 设置标头,如下所示:header('Content-Type: application/json');我已经在Firebug和Chrome的开发工具中检查了响应标头;在两种情况下均已正确设置.删除该错误会在Firefox版本中隐藏该错误消息,而纯文本不是等宽字体,仅此而已.

I am setting the header, like so: header('Content-Type: application/json'); I've checked the response header in Firebug and Chrome's development tools; it is correctly set in both cases. Removing that hides the error message in the Firefox version and the plaintext is not in a monospace font, but that's it.

完整的请求标头:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:msgPublishID=1347362550,1345649049; logout_rem=1; sh_rand=625703e7f9f9e03efabaef56f52ff97d7f68bc67; username=kryan; password=f85720746a490ece4dd7a945f5c9ed8e25b15f1f; fullname=Kevin+Ryan; user_type=1
Host:localhost
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1

完整的响应头:

Connection:Keep-Alive
Content-Length:371
Content-Type:application/json
Date:Thu, 27 Sep 2012 19:12:52 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4
X-Powered-By:PHP/5.4.4

我在JSON本身上经历了许多变体,但是当这样简单的事情出现时,我无法想象这是JSON的问题:

I've gone through many variations on the JSON itself, but I can't imagine that it's the JSON's problem when something as simple as this:

{"session":"expired"}

仍然失败.我已经反复检查过;从字面上看,这是服务器响应的全部,但是JSON View仍然会抱怨.对于更复杂的JSON,我一直在使用

is still failing. I've checked repeatedly; that is literally the entirety of the server's response, but JSON View still complains. For more complicated JSONs, I've been using

echo json_encode($output, JSON_PRETTY_PRINT);

其中$output是一个关联数组;输出看起来完全正确,但是JSON View仍在抱怨.这是文件中唯一未注释掉的echo.

where $output is an associative array; the output looks completely right but JSON View is still complaining. This is the only echo in the file that isn't commented out.

那么,这到底是怎么回事?我真的需要JSON View;我一直在使用非常大的JSON,并且折叠和扩展对象和数组的能力对于调试我的应用程序至关重要. 此在线JSON查看器似乎可以使用,但是如果我必须复制并粘贴,我的工作效率将会受到打击每次测试它们时,这些PHP文件的输出.

So what on earth could be going wrong here? I really need JSON View; I work with very large JSONs constantly and the ability to collapse and expand objects and arrays is critical to debugging my application. This online JSON viewer seems to work, but my productivity is going to take a hit if I have to copy and paste the output of these PHP files every time I'm testing them.

我发现可行的一件事是,如果我这样做:

One thing I have found that works is if I do this:

<?php
    header('Content-Type: application/json');
    die('{"debug":true}');
    // remainder of the program as-is, starting with...
    require('dbinfo.php');

如果我再这样做:

<?php
    header('Content-Type: application/json');
    require('dbinfo.php'); // note this comes before the die statement
    die('{"debug":true}');
    // remainder of the program as-is

我再次收到错误消息.

因此,这表明dbinfo.php引起了问题.

So this implies that dbinfo.php is causing the problem.

对不起,我已经从这个问题中删除了dbinfo.php,因为它可能包含不应该公开的敏感数据(即使我删除了明显的内容).由于dbinfo.php content 不相关,因此删除它似乎更安全.请参阅下面的答案.

Sorry, I've removed dbinfo.php from this question because it might possibly contain sensitive data that should not be public (even though I stripped out the obvious things). Since the content of dbinfo.php wasn't relevant, it just seems safer to remove it. See my answer below.

推荐答案

Argh.

我知道了:BOM搞砸了,但是当然它还是完全看不见的.在没有BOM的情况下检查编码并将其更改为UTF-8,可以完全解决此问题.

I figured it out: the BOM was screwing things up, but of course it was also being completely invisible. Checking and changing the encoding to UTF-8 without BOM fixed the problem entirely.

我不知道BOM表是PHP设计还是Unicode设计的问题,但这肯定令人讨厌.

I don't know if this BOM thing is a problem with PHP's design or Unicode's design, but it is certainly obnoxious.

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

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