【问题标题】:On Bing Ads Api how can I convert the results to Json or Xml?在 Bing Ads Api 上,如何将结果转换为 Json 或 Xml?
【发布时间】:2019-12-15 23:02:56
【问题描述】:

我正在为 Bing Ads 使用 Python Api,我想将所有活动、广告等存储在数据库中,并从现在开始与 Bing 广告同步。我想将活动存储在 DB 中,以 xml 或 json 格式存储,所以基本上序列化对象。

所以如果我尝试这样做:

json.dumps(campaign)
# TypeError: Object of type Campaign is not JSON serializable

我在 stackoverflow 上阅读并尝试过这样的事情:

json.dumps(campaign.__dict__)
# TypeError: Object of type __printer__ is not JSON serializable

所以我开始删除对象的不可序列化的属性,但这听起来不对,必须有更优雅的方式..

经过不同的尝试,我尝试过的越来越少


        campaigns = campaign_service.GetCampaignsByAccountId(
            AccountId=account_id,
            CampaignType=bc.ALL_CAMPAIGN_TYPES)

        for campaign in campaigns['Campaign']:
            dir(campaign)
            if campaign.Id is None:
                continue
            print(campaign.__printer__.tostr())
            exit()
            # delattr(campaign.__dict__, "ManualCpcBiddingScheme")
            delattr(campaign, "__metadata__")
            delattr(campaign, "__printer__")
            print(campaign.__dict__)
            rows.append(json.dumps(campaign.__dict__))
            exit()
            print(rows)
            exit()

不幸的是,我在 bing、toxml 或 tojson 中找不到任何“本机”方法。 此外,Bing 在 API 方面并不那么受欢迎,因此网上的信息并不多。

【问题讨论】:

    标签: python json xml bing bing-api


    【解决方案1】:

    你应该实现你自己的json.JSONEncoder,它会处理像__printer__这样的特殊字段

    https://docs.python.org/3/library/json.html

    有关示例,请参阅 https://www.programcreek.com/python/example/576/json.JSONEncoder

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多