以下是我所拥有代码的摘录。
fullsrcurl = self.srcjson + format_type + '&jobname=' + joburl
srcfile = urllib2.urlopen(fullsrcurl)
if self.format_export == 'json':
srcdata = json.load(srcfile)
pprint(srcdata)
srcjobmst_id = srcdata["jobmst_id"][0]
我尝试使用上述代码的内容是从API网址中读取的,该网址会返回json并获取' jobmst_id'来自那个json的价值。 pprint(srcdata在下面提供了json)我想从中拉出jobmst_id值,在下面的例子中将是' 58908'。我很乐意将它作为一个可以转换的整数,但是就目前看来它在主题上的错误就失败了。
以下是json -
[{u'evntmst_id': {u'evntmst_calendar': 0,
u'evntmst_crttm': u'2009-12-04T11:22:03',
u'evntmst_desc': None,
u'evntmst_fiscal': 0,
u'evntmst_frcstdt': u'2012-12-31T00:00:00',
u'evntmst_freq': 1,
u'evntmst_fromdt': None,
u'evntmst_id': 1,
u'evntmst_intsect': u'N',
u'evntmst_lstchgtm': u'2009-12-04T12:28:52',
u'evntmst_lstcmptm': u'2014-08-28T12:00:29',
u'evntmst_monthdays': u'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
u'evntmst_months': u'YYYYYYYYYYYY',
u'evntmst_name': u'Daily
',
u'evntmst_occur': 0,
u'evntmst_offset': 0,
u'evntmst_owner': 2,
u'evntmst_public': u'Y',
u'evntmst_subtype': None,
u'evntmst_timeframe': 0,
u'evntmst_totcnt': None,
u'evntmst_type': 2,
u'evntmst_untildt': u'2012-12-31T00:00:00',
u'evntmst_weekdays': u'YYYYYYY',
u'evntmst_weeks': u'NNNNN'},
u'job_dependencies': [],
u'job_events': [],
u'jobcls_id': None,
u'jobdtl_id': {u'jobdtl_abrtonclderr': None,
u'jobdtl_allowadhoc': u'Y',
u'jobdtl_carryover': 1,
u'jobdtl_cmd': None,
u'jobdtl_concur': 1,
u'jobdtl_deplogic': 1,
u'jobdtl_deprerun': u'N',
u'jobdtl_duration': 67,
u'jobdtl_envfile': None,
u'jobdtl_estdurexclude': 4,
u'jobdtl_estmethod': 1,
u'jobdtl_extinfo': None,
u'jobdtl_failalarm': None,
u'jobdtl_fromdt': u'2012-11-22T00:00:00',
u'jobdtl_fromtm': u'1899-12-30T23:30:00',
u'jobdtl_id': 58908,
u'jobdtl_inhagent': u'N',
u'jobdtl_inhevent': u'N',
u'jobdtl_inhoptions': u'N',
u'jobdtl_inhrepeat': u'N',
u'jobdtl_inhtime': u'N',
u'jobdtl_interval': None,
u'jobdtl_intervalcnt': None,
u'jobdtl_maxrun': 60,
u'jobdtl_minrun': 60,
u'jobdtl_nearoutage': 3,
u'jobdtl_normalexit': 0,
u'jobdtl_normalop': 1,
u'jobdtl_normalrange': 0,
u'jobdtl_outputname': None,
u'jobdtl_params': None,
u'jobdtl_priority': 50,
u'jobdtl_proxy': 4,
u'jobdtl_proxy2': None,
u'jobdtl_psjob': None,
u'jobdtl_rerun': None,
u'jobdtl_rerunok': u'Y',
u'jobdtl_retnsn': 30,
u'jobdtl_sapcount': None,
u'jobdtl_savelogonly': u'N',
u'jobdtl_saveoutput': u'Y',
u'jobdtl_statuscl': None,
u'jobdtl_timewin': 2,
u'jobdtl_trackcl': None,
u'jobdtl_trackcmd': None,
u'jobdtl_trackmethod': 1,
u'jobdtl_trxid': None,
u'jobdtl_unit': None,
u'jobdtl_untildt': u'1899-12-30T00:00:00',
u'jobdtl_untiltm': u'1899-12-30T23:50:00',
u'jobdtl_waitop': u'N',
u'jobdtl_workdir': None,
u'nodlstmst_id': 27,
u'nodmst_id': None,
u'servicemst_id': None},
u'jobmst_active': u'Y',
u'jobmst_alias': u'58908 ',
u'jobmst_crttm': u'2012-11-22T12:22:59',
u'jobmst_desc': None,
u'jobmst_dirty': u' ',
u'jobmst_evntoffset': None,
u'jobmst_id': 58908,
u'jobmst_lstchgtm': u'2012-11-22T13:01:34',
u'jobmst_mode': 0,
u'jobmst_name': u'SWIFT DB Backup',
u'jobmst_owner': {u'owner_allagents': u'Y',
u'owner_id': 257,
u'owner_name': u'Common ',
u'owner_type': 2},
u'jobmst_prntid': None,
u'jobmst_prntname': None,
u'jobmst_type': 1,
u'runbook_url': None}]
答案 0 :(得分:1)
检索数据的方法是srcdata[0]["jobmst_id"]
而不是srcdata["jobmst_id"][0]
。