【发布时间】:2015-02-26 11:38:22
【问题描述】:
我有一个获取 json 数据的 python 爬虫和使用这些数据的 java 程序。我的方法是将 json 数据序列化到一个文件中。它在 python 中被表示为字典,我做到了:
mails_file.write(str(email)+'\n')
这将产生如下结果:
{u'from': u'Brian Yablonski <brian@jeb.org>', u'dateCentral': u'1999-01-05T07:33:06-06:00', u'to': u'"\'jeb@jeb.org\'" <jeb@jeb.org>', u'date': u'Tue, 5 Jan 1999 08:33:06 -0500', u'message': u"Missed the deadline, but I would have said the speech is a first step \ntoward restoring the rightful place of communities and families as the \nfirst source of ideas and solutions to our society's problems and to show \nthat government can work not as a master of, but a partner with, these \ninstitutions.\n\n-----Original Message-----\nFrom:\tJeb Bush [SMTP:jeb@jeb.org]\nSent:\tMonday, January 04, 1999 3:44 PM\nTo:\tYablonski Brian\nSubject:\tFW: Speech\n\nHow would you describe the speech for Mark?\n\n-----Original Message-----\nFrom:\tMark Silva [mailto:bureau@tdo.infi.net]\nSent:\tMonday, January 04, 1999 3:14 PM\nTo:\tJeb@jeb.org\nSubject:\tSpeech\n\nHave a quick thematic description for me about what you hope to accomplish\nwith tomorrow's inaugural address? (If you see this note before 6:30). If\nnot, thanks anyway and I wish you well Tuesday.", u'id': u'19990105-01BE3886.0851BC20.brian@jeb.org', u'subject': u'RE: Speech'}
那我想做一些python-java格式调整比如:
line = line.replace('u"', '"')
line = line.replace("u'", '"')
line = line.replace("'", '"')
最后在 java 中加载 JSON 对象:
JSONObject lineJson = new JSONObject(line);
由于 python 可以互换使用 " 和 ' 字符,这种方法在我 95% 的对象上都失败了。我怎样才能克服这个问题并编写一个可以实际工作的格式转换函数?
【问题讨论】:
标签: java python json serialization