這裏是我的JSON響應檢查是否JSON響應行存在與否和Python
{u'kind ':u'bigquery#queryResponse' u'rows「:[{u'f「: [{UV:U'1 '},{UV:u'1607'},{UV:u'coriolanus '}]},{u'f':[{UV ':U'1'},{UV:u'1596 '},{UV:u'kingjohn'}]},{u'f「:[{UV:U'1 '},{UV:u'1599'},{UV:u'kinghenryv '}]},{u'f':[{UV:U'1「},{藍ν:u'1600 '},{UV:u'merrywivesofwindsor'}]},{u'f ':[{UV:U'1'},{UV我'1602'},{UV:u'troilusandcressida '}]},{u'f':[{UV:U'1 '},{UV:u'1592'}, {UV:u'comedyoferrors '}]},{u'f':[{UV:U'2 '},{UV:u'1590'},{UV :u'3kinghenryvi '}]},{u'f':[{UV:U'2 '},{UV:u'1612'},{UV:u'kinghenryviii' }]},{u'f ':[{UV:U'2'},{UV:u'1598 '},{UV:u'2kinghenryiv'}]}], u'jobRefe天信 ':{u'projectId':u'1039435439624 'u'jobId':u'job_ffb30cfb23674f88aa5cb497e358ec05 '},u'jobComplete':真,u'totalRows ':u'9' u'schema ':{在'字段:[{u'type ':u'INTEGER',u'name ':u'sum_for_the',u'mode ':u'NULLABLE'},{u'type ':u'INTEGER',u'name ':u'corpus_date',u'mode ':u'NULLABLE'},{u'type ':u'STRING',u'name ':u'f0_',u'mode ':u'NULLABLE'}] }}
和使用該環路通過下面
resp = []
for row in listReply['rows']:
for key,dict_list in row.iteritems():
count = dict_list[0]
year = dict_list[1]
corpus = dict_list[2]
resp.append({'count': count['v'],'year':year['v'],'corpus':corpus['v']})
的Python代碼如何檢查是否此listReply['rows']
存在與否,如如下面JSON
{u'totalRows ':u'0' u'kind ':u'bigquery#queryResponse' u'jobComplete ':真,u'jobReference':{u'projectId ':u'1039435439624' u'jobId ':u'job_8efc645852c34515bcff4ab3969772fd'},u'schema ':{u'fields':[{u'type ':u'INTEGER',u'name ':u'sum_for_the',u'mode」在'可爲空'},{u'type ':u'INTEGER',u'name ':u'corpus_date',u'mode ':u'NULLABLE'},{u'type ':u'STRING',U'名 ':u'f0_' u'mode ':u'NULLABLE'}]}}
但第二種方法什麼是進入for循環之前的行 – iJade
@jade,抱歉,這是一個錯字。這就是爲什麼我喜歡前者 - 減少犯錯誤的機會:) –