简体   繁体   English

如何将 json 转换为 Python 列表?

[英]How to convert json to Python List?

I want to get data in form of list while using pycurl but it's showing an object of json :( Could you please tell me simplest way how to break and get list out of it? Thanks in advance for guiding :) Here is my code:我想在使用 pycurl 时以列表的形式获取数据,但它显示了一个 json 对象:( 你能告诉我如何打破并从中获取列表的最简单方法吗?在此先感谢您的指导:) 这是我的代码:

import pycurl 
import json  
from io import BytesIO 

c = pycurl.Curl()
data = BytesIO()

c.setopt(c.URL,'https://api.angel.co/1/jobs')
c.setopt(c.WRITEFUNCTION, data.write) 
c.perform()
fetched_data= data.getvalue()
print (fetched_data); 

Decode the json with the json module you already imported:用你已经导入的json模块解码 json:

result = json.loads(fetched_data.decode('utf8'))

I hardcoded the encoding here;我在这里硬编码的编码; the JSON RFC states UTF-8 is the default, but you could see if the Content-Type response header has a charset parameter that tells you the actual encoding. JSON的RFC规定UTF-8是默认的,但你可以看到,如果Content-Type响应头有一个charset ,告诉你实际的编码参数。

I'd not use pycurl ;我不使用pycurl ; it is really, really cumbersome to deal with.处理起来真的非常麻烦 Use the requests library instead, it can handle JSON for you, out of the box.使用requests相反,它可以处理JSON你,开箱即用。 It handles decoding of the bytestream to Unicode for you :它处理的字节流为Unicode为您解码:

import requests

result = requests.get('https://api.angel.co/1/jobs').json()

That specific URL returns a JSON object , resulting in a Python dictionary:这特定的URL返回一个JSON对象,导致Python字典:

{'jobs': [{'angellist_url': 'https://angel.co/jobs?startup_id=267120',
            'created_at': '2014-02-06T08:07:35Z',
            'equity_cliff': '0.0',
            'equity_max': '0.0',
            'equity_min': '0.0',
            'equity_vest': '0.0',
            'id': 22672,
            'salary_max': 0,
            'salary_min': 0,
            'startup': {'angellist_url': 'https://angel.co/nationsroot-1',
                         'community_profile': False,
                         'company_url': 'http://nationsroot.com',
                         'created_at': '2013-09-20T07:55:25Z',
                         'follower_count': 5,
                         'hidden': False,
                         'high_concept': 'Bridge between citizens and politicians',
                         'id': 267120,
                         'logo_url': 'https://s3.amazonaws.com/photos.angel.co/startups/i/267120-9513670dbfe74c170201df0e385d1c2c-medium_jpg.jpg?buster=1379663721',
                         'name': 'NationsRoot',
                         'product_desc': 'NationsRoot helps you find profiles and report cards of politicians, share your thoughts and rate your political leaders. We believe that citizens are not absolved of their duties once they are done voting. So, We provide a platform where you can rate the quality of government provided services to create real time Report Cards for all politicians.\r\n\r\nOn the other hand, Politicians will have the detail analytics about requirements of citizens in various electoral area which will be helpful during elections and can have latest updates too.',
                         'quality': 3,
                         'thumb_url': 'https://s3.amazonaws.com/photos.angel.co/startups/i/267120-9513670dbfe74c170201df0e385d1c2c-thumb_jpg.jpg?buster=1379663721',
                         'updated_at': '2014-02-06T07:36:36Z'},
            'tags': [{'angellist_url': 'https://angel.co/business-development-1',
                       'display_name': 'Business Development',
                       'id': 15525,
                       'name': 'business development',
                       'tag_type': 'SkillTag'},
                      {'angellist_url': 'https://angel.co/sales-strategy-and-management',
                       'display_name': 'Sales Strategy and Management',
                       'id': 16928,
                       'name': 'sales strategy and management',
                       'tag_type': 'SkillTag'},
                      {'angellist_url': 'https://angel.co/sales-and-marketing-2',
                       'display_name': 'Sales and Marketing',
                       'id': 23989,
                       'name': 'sales and marketing',
                       'tag_type': 'SkillTag'},
                      {'angellist_url': 'https://angel.co/australia',
                       'display_name': 'Australia',
                       'id': 1618,
                       'name': 'australia',
                       'tag_type': 'LocationTag'},
                      {'angellist_url': 'https://angel.co/sales-2',
                       'display_name': 'Sales',
                       'id': 80488,
                       'name': 'sales',
                       'tag_type': 'RoleTag'}],
            'title': 'Sales Intern',
            'updated_at': '2014-02-06T08:07:57Z'},
           # many more entries elided
           ],
 'last_page': 184,
 'page': 1,
 'per_page': 50,
 'total': 9195}

The list you are looking for then is result['jobs'] , but you'll also have to request additional pages to get all 9195 results.你正在寻找,然后名单result['jobs']但你也有权要求额外的页面来获得所有9195个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM