如何使用python打印完整的json数组?
我有一个json数组。并且只需要使用python打印id。我该怎么做 这是我的json数组:如何使用python打印完整的json数组?,python,json,Python,Json,我有一个json数组。并且只需要使用python打印id。我该怎么做 这是我的json数组: { "messages": [ { "id": "1531cf7d9e03e527", "threadId": "1531cf7d9e03e527" }, { "id": "1531cdafbcb4a0e6", "threadId": "1531bfccfceb1ed7" } ], "nextPageToken": "01647645424
{
"messages":
[
{
"id": "1531cf7d9e03e527",
"threadId": "1531cf7d9e03e527"
},
{
"id": "1531cdafbcb4a0e6",
"threadId": "1531bfccfceb1ed7"
}
],
"nextPageToken": "01647645424797380808",
"resultSizeEstimate": 103
}
*编辑:*
实际上,我正在编写一个python代码来从gmail API获取消息。
该程序以json格式提供gmail帐户消息的消息ID和线程ID。我只需要消息id,不需要线程id
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
import json
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
CLIENT_SECRET_FILE = 'client_server.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'gmail-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else:
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
message = service.users().messages().get(userId='me',id='').execute()
response = service.users().messages().list(userId='me',q='').execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
print(messages)
result = loads(messages)
ids = [message['id'] for message in result['messages']]
print (ids)
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId='me', q='',pageToken=page_token).execute()
messages.extend(response['messages'])
print(message['id'])
print (message['snippet'])
if __name__ == '__main__':
main()
TypeError:应为字符串或缓冲区。。它给了我raw_decode()error@andreyIt,看起来您传递的是python列表而不是JSON字符串(就像您在回答中所说的)。如果是这样,只需跳过json.loads部分,并在列表中使用列表迭代器<代码>[message['id']用于您的_-parsed_-json['messages']].在您的caseTypeError中:需要字符串或缓冲区。。它给了我raw_decode()error@andreyIt,看起来您传递的是python列表而不是JSON字符串(就像您在回答中所说的)。如果是这样,只需跳过json.loads部分,并在列表中使用列表迭代器
[message['id']对于您的_-parsed_-json['messages']]]
在您的案例中,请为您提议的解决方案添加一些说明。请为您提议的解决方案添加一些说明。
import json
dict_result = json.loads(your_json)
ids = [message['id'] for message in dict_result['messages']]
messages=[]
store=[]
if 'messages' in response:
messages.extend(response['messages'])
for i in range(len(messages)):
//to store the id alone (Taking the id from the json array)
store=messages[i]['id']
//retrieve messages of this id
message = service.users().messages().get(userId='me',id=store).execute()
print(store)