JSON is invalid (JSONview), but I don't see how

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

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.

The Firefox version has an error message:

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

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

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.

Complete request headers:

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 

Complete response headers:

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 

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"} 

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

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.

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.

EDIT: 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'); 

If I then go with this:

<?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 

I get the error again.

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

EDIT: 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.

回答1:

Argh.

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.

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!