0
以下是我擁有的代碼片段。TypeError:列表索引必須是整數,而不是來自JSON文件的str
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]
我試圖與上面的代碼做的是從被返回的JSON,並得到來自JSON的「jobmst_id」值的API網址讀取。 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}]
你不包括JSON的開始,因爲必須有一個開始}和]使其有效。這就回答了你的問題:它作爲一個帶有字典的列表來回放,而不僅僅是一本字典。你想做'srcdata [0] [「jobmst_id」]',而不是'jobmst_id [「jobmst_id」] [0]'。 – 2014-09-01 04:43:27
json有效。我更新了完整的文件。我不得不削減它,因爲它抱怨太多的代碼。 – whoisearth 2014-09-01 05:03:03