我有这样old.JSON
文件:写protobuf的对象到JSON文件
[{
"id": "333333",
"creation_timestamp": 0,
"type": "MEDICAL",
"owner": "MED.com",
"datafiles": ["stomach.data", "heart.data"]
}]
然后,我创建基于.proto
文件的对象:
message Dataset {
string id = 1;
uint64 creation_timestamp = 2;
string type = 3;
string owner = 4;
repeated string datafiles = 6;
}
现在我要救这个对象救回来这个对象到其他.JSON
文件。 我这样做:
import json
from google.protobuf.json_format import MessageToJson
with open("new.json", 'w') as jsfile:
json.dump(MessageToJson(item), jsfile)
因此,我有:
"{\n \"id\": \"333333\",\n \"type\": \"MEDICAL\",\n \"owner\": \"MED.com\",\n \"datafiles\": [\n \"stomach.data\",\n \"heart.data\"\n ]\n}"
如何使这个文件看起来像old.JSON
文件?
以何种方式在此不喜欢原来的?我注意到它不在列表中。这是问题吗? – tdelaney
@tdelaney是的,它不是一个列表。它具有“而不仅仅是”,并且\ n是明确的。 –
您是否直接试过'jsfile.write(MessageToJson(item))'? – Psidom