在 Google App Engine Web 应用程序中单引号替换双引号
我最近在 Google App Engine 中进行了大量编码。令我惊讶的是,当使用 simplejson 模块时,代码突然开始表现得很奇怪。虽然应用程序的一个实例将打印(使用 self.response.out.write(serialized)
其中 serialized
是一个字典字符串)JSON 格式为:
{"error": "Your user key does not exist"}
生产实例将打印将 JSON 字典字符串输出为:
{'error': 'Your user key does not exist'}
显然,后一种是不正确的,因为它使用单引号,而不是双引号。 (因此 JSONLint 或几乎任何 JSON 解析在解析时都会消失)
最有趣的部分?当使用 logging.info('')
打印到控制台时,两者都会正确打印 JSON。除了用于测试的打印代码之外,我注释掉了生产代码中的几乎所有内容,但问题仍然存在。
到底是怎么回事?!是否有一个神奇的开关在打印到屏幕上时可以用单引号替换所有漂亮的双引号?
为 Stack Overflowers 的娱乐而添加:
在我的 GAE 实例以及公共服务器上执行的下面的代码也将生成带单引号的 JSON,从而使其成为最简单的示例。
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import simplejson as json
class MainHandler(webapp.RequestHandler):
def get(self):
testing = { "testing" : True, "why?" : 123 }
serialized = json.dumps(testing)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(testing)
def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
I have been coding extensively in Google App Engine recently. To my surprise, when using the simplejson module, the code started suddenly behaving weirdly. While one instance of the app will print (using self.response.out.write(serialized)
where serialized
is a dictionary string) JSON formatted as:
{"error": "Your user key does not exist"}
the production one will print out the JSON dictionary string as:
{'error': 'Your user key does not exist'}
Obviously, the latter one is incorrect, as it uses single quotes, instead of double quotes. (and thus JSONLint or pretty much any JSON parses dies when parsing it)
Funniest part? When printed to console using logging.info('')
both print the JSON correctly. I commented out pretty much everything in the production code except for the printing code for testing and the problem remains.
What is going on?! Is there a magic switch somewhere that replaces all the nice double quotes with single quotes when printed to the screen?!
Added for the entertainment of Stack Overflowers:
The code below, executed on my instance of GAE as well as the public server will also produce JSON with single quotes, thus making this the simplest example possible.
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import simplejson as json
class MainHandler(webapp.RequestHandler):
def get(self):
testing = { "testing" : True, "why?" : 123 }
serialized = json.dumps(testing)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(testing)
def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在我只觉得自己很愚蠢。
问题是我从未将序列化代码写入屏幕,而是写入数据数组本身。因此,要从上面的代码中获得功能齐全的示例,必须将 self.response.out.write(testing)< 中的
testing
替换为serialized
/代码>。TL;DR:仔细检查您的代码以及您要打印到屏幕上的内容,孩子们。
And now I just feel dumb.
The problem was that I never wrote the serialized code to the screen, but rather the data array itself. Thus, to get a fully functional example from the code above, one has to replace
testing
withserialized
in theself.response.out.write(testing)
.TL;DR: Double check your code and what you are printing to screen, kids.