Python 3.6
我有一个json对象,其字符串表示如下:
json.dumps(sample_data)
{"State": "OFF", "RepresentedBy": {"Agency": null, "Manager": null}, "NegativeKeywords": {"Items": ["rain", "thunder", "lightning"]}, "StartDate": "2019-06-16", "TimeZone": "Europe/Paris", "Funds": {"Mode": "SHARED_ACCOUNT_FUNDS", "SharedAccountFunds": {"Refund": 0, "Spend": 0}}, "DailyBudget": null, "StatusPayment": "ALLOWED", "Id": 44049374, "StatusClarification": "Accepted by moderator", "EndDate": null, "Notification": {"SmsSettings": {"TimeTo": "21:00", "TimeFrom": "09:00"}, "EmailSettings": {"SendAccountNews": "YES", "CheckPositionInterval": 60, "Email": "petrov.context.ads@yandex.ru", "WarningBalance": 20, "SendWarnings": "NO"}}, "BlockedIps": {"Items": ["76.239.56.11", "93.185.26.46"]}, "Name": "\u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440/\u041f\u041e\u0418\u0421\u041a/\u041c\u043e\u0441\u043a\u0432\u0430", "TextCampaign": {"Settings": [{"Value": "NO", "Option": "ADD_TO_FAVORITES"}, {"Option": "REQUIRE_SERVICING", "Value": "NO"}, {"Value": "YES", "Option": "SHARED_ACCOUNT_ENABLED"}, {"Option": "DAILY_BUDGET_ALLOWED", "Value": "YES"}, {"Value": "YES", "Option": "MAINTAIN_NETWORK_CPC"}, {"Value": "NO", "Option": "ENABLE_SITE_MONITORING"}, {"Option": "ADD_METRICA_TAG", "Value": "YES"}, {"Option": "ADD_OPENSTAT_TAG", "Value": "NO"}, {"Value": "YES", "Option": "ENABLE_EXTENDED_AD_TITLE"}, {"Option": "ENABLE_COMPANY_INFO", "Value": "YES"}, {"Value": "NO", "Option": "EXCLUDE_PAUSED_COMPETING_ADS"}, {"Option": "ENABLE_AREA_OF_INTEREST_TARGETING", "Value": "YES"}], "PriorityGoals": null, "BiddingStrategy": {"Network": {"BiddingStrategyType": "SERVING_OFF"}, "Search": {"BiddingStrategyType": "AVERAGE_CPC", "AverageCpc": {"AverageCpc": 20000000, "WeeklySpendLimit": null}}}, "RelevantKeywords": {"OptimizeGoalId": 0, "BudgetPercent": 40}, "CounterIds": {"Items": [54087493]}}, "Currency": "RUB", "Statistics": {"Clicks": 0, "Impressions": 0}, "ClientInfo": "\u041f\u0435\u0442\u0440\u043e\u0432 \u041c\u0438\u0445\u0430\u0438\u043b", "Status": "ACCEPTED", "Type": "TEXT_CAMPAIGN", "TimeTargeting": {"HolidaysSchedule": null, "ConsiderWorkingWeekends": "YES", "Schedule": {"Items": [["1", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100"], ["2", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100"], ["3", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100"], ["4", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100"], ["5", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100"], ["6", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100"], ["7", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100", "100"]]}}, "SourceId": null, "ExcludedSites": null}
好吧,在Python中,它只是字典,列表和字符串的字典。
我想创建一个函数来在此json对象内按“路径”提取数据。
path = '["TextCampaign"]["Settings"][0]["Option"]'
def extract_data(json_object, path):
# The code
return data
assert sample_data["TextCampaign"]["Settings"][0]["Option"]==extract_data(json_object, path)
也许json库中的某些功能可以做到这一点。您能帮我完成这个任务吗?
答案 0 :(得分:0)
我建议使用JSONPath语法(基于XPath):https://jsonpath.com/
有一个实现此功能的python库:https://pypi.org/project/jsonpath/
path = 'TextCampaign.Settings[0].Option'
这等同于:
sample_data["TextCampaign"]["Settings"][0]["Option"]
您可以在这里尝试:https://jsonpath.com/