概述
我通过API从外部系统(Salesforce Marketing Cloud)获取一些数据,我将以下面的格式获取数据:
Results: [(List){
Client =
(ClientID){
ID = 113903
}
PartnerKey = None
CreatedDate = 2013-07-29 04:43:32.000073
ModifiedDate = 2013-07-29 04:43:32.000073
ID = 1966872
ObjectID = None
CustomerKey = "343431CD-031D-43C7-981F-51B778A5A47F"
ListName = "PythonSDKList"
Category = 578615
Type = "Private"
Description = "This list was created with the PythonSDK"
ListClassification = "ExactTargetList"
}]
它非常接近JSON,我想将其格式化为JSON文件,以便将其导入我们的数据库.有关如何做到这一点的任何想法?
谢谢
from suds.sudsobject import asdict
def recursive_asdict(d):
out = {}
for k,v in asdict(d).iteritems():
if hasattr(v,'__keylist__'):
out[k] = recursive_asdict(v)
elif isinstance(v,list):
out[k] = []
for item in v:
if hasattr(item,'__keylist__'):
out[k].append(recursive_asdict(item))
else:
out[k].append(item)
else:
out[k] = v
return out
def suds_to_json(data):
return json.dumps(recursive_asdict(data))
第一个将它转换为dict,第二个转换为正确的json.
一些有用的链接:
https://fedorahosted.org/suds/wiki/Documentation
总结
以上是编程之家为你收集整理的python – 将字符串输出转换为JSON全部内容,希望文章能够帮你解决python – 将字符串输出转换为JSON所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧