您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 将字符串输出转换为JSON

5b51 2022/1/14 8:21:54 python 字数 2777 阅读 515 来源 www.jb51.cc/python

我通过API从外部系统(Salesforce Marketing Cloud)获取一些数据,我将以下面的格式获取数据:Results: [(List){ Client = (ClientID){ ID = 113903 } PartnerKey = None CreatedDate = 2013-0

概述

我通过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文件,以便将其导入我们的数据库.有关如何做到这一点的任何想法?

谢谢

suds对象已经为你做了.只需调用您要查找的属性即可.

但是,如果你想要它作为一个dict,附加是它的常用功能

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所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶