gpt4 book ai didi

java - JSON : serialization from python to Java

转载 作者:行者123 更新时间:2023-11-30 03:32:18 24 4
gpt4 key购买 nike

我有一个获取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% 的对象上失败。我怎样才能克服这个问题并编写一个格式转换函数真的会起作用吗?

最佳答案

您需要使用json模块或等效模块:

import json
# ...
json.dump(email, mails_file)

来自有关 json.dump 的文档:

Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using this conversion table.

关于java - JSON : serialization from python to Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28741444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com