繁体   English   中英

使用Python解码JSON

[英]Decoding JSON with Python

我从Python中的Web请求获得了JSON输出,但是我很难将这些数据转换为Python字典。

如果您在此处查看获取属性模型: https : //docs.simplyrets.com/api/index.html#!/default/get_properties

我的目的是解码JSON输出并将其放入字典中。 现在我不确定如何执行此操作。 显然,将JSON输出输入到类似http://jsonprettyprint.com/的网站中应该可以使其更易于阅读,但无法正常工作。 输出只是JSON代码的混搭,我不知道有什么简单的方法可以看到它。 要很好地查看此输出,它可以帮助我构建for循环,这是我认为需要的。

我想我需要基于上述模型从当前属性名称中提取的所有相关属性值创建一个for循环? 有没有更简单的方法可以做到这一点。

[{u'office': {u'contact': None, u'brokerid': None, u'name': None, u'servingName': None}, u'listDate': u'2011-05-23T18:50:30.184Z', u'sales': {u'contractDate': None, u'closeDate': u'1996-10-21T15:15:54.171Z', u'agent': {u'lastName': u'Long', u'contact': None, u'id': u'blong', u'firstName': u'Buffy'}, u'office': {u'contact': None, u'brokerid': u'ACME12', u'name': u'Acme Brokerage', u'servingName': u'Acme Brokerage'}, u'closePrice': 17946033}, u'agent': {u'lastName': u'Phelps', u'contact': None, u'id': u'sphelps', u'firstName': u'Shoshana'}, u'leaseTerm': None, u'listingId': u'49699701', u'privateRemarks': u'This property is a trial property to test the SimplyRETS. Private agent remarks will be included in this field for use in the SimplyRETS REST API. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', u'showingInstructions': u'The showing instructions for this trial property are brought to you by the SimplyRETS team. This field will include any showing remarks for the given listing in your RETS feed. Enjoy!', u'terms': u'Conventional', u'tax': {u'id': u'593-723-781-8056'}, u'photos': [u'https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home9.jpg', u'https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home-inside-9.jpg'], u'address': {u'city': u'Houston', u'full': u'74434 East Sweet Bottom Br #18393', u'streetNumber': 74434, u'country': u'United States', u'state': u'Texas', u'unit': u'18393', u'crossStreet': u'Dunne Ave', u'postalCode': u'77096', u'streetName': u'East Sweet Bottom Br'}, u'remarks': u'This property is a trial property to test the SimplyRETS. This field will include remarks or descriptions from your RETS feed intended for public view. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', u'geo': {u'county': u'North', u'lat': 29.689418, u'lng': -95.474464, u'directions': u'From 290 exit Barker Cypress to left on Tuckerton, right on Danbury Bridge, right on Bending Post, right on Driftwood Prairie', u'marketArea': u'Spring/Klein'}, u'school': {u'highSchool': u'Cy Creek', u'district': None, u'elementarySchool': u'HAYES', u'middleSchool': u'FALCON PASS'}, u'modified': u'2015-11-14T17:57:26.262Z', u'mls': {u'status': u'Active', u'daysOnMarket': 862, u'area': u'Spring/Klein'}, u'leaseType': u'FullServ', u'property': {u'bathrooms': None, u'laundryFeatures': u'Area,Electric Dryer Hookup,Individual Room,Washer Hookup', u'lotDescription': u'Private Backyard', u'subType': None, u'additionalRooms': u'Recreation,Master Bedroom,See Remarks', u'areaSource': u'Floorplans', u'style': u'Traditional', u'flooring': None, u'area': 1043, u'exteriorFeatures': u'Back Yard Fenced, Covered Patio/Deck, Patio/Deck', u'cooling': None, u'yearBuilt': 1998, u'lotSize': u'127X146', u'type': u'RES', u'foundation': u'Slab', u'fireplaces': 1, u'occupantType': None, u'bedrooms': 2, u'interiorFeatures': u'Drapes/Curtains/Window Cover, Fire/Smoke Alarm, High Ceiling, Island Kitchen', u'accessibility': u'Manned Gate', u'occupantName': None, u'construction': u'In Kitchen,Stackable,Washer Included', u'bathsHalf': 6, u'water': None, u'subdivision': u'Waterstone Springs', u'bathsFull': 5, u'heating': u'Radiant Heat', u'roof': u'Tile', u'stories': 3, u'view': u'Golf Course'}, u'mlsId': 1005192, u'listPrice': 20714261, u'disclaimer': u'This information is believed to be accurate, but without warranty.'}]

编辑:大家好,

抱歉,我已经尝试过了

>>> parsed_json = json.loads(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())

TypeError:预期的字符串或缓冲区

json.loads应该将JSON字符串转换成字典。

您需要先import json 有关更多信息,请参见https://docs.python.org/2/library/json.html

>>> json.loads('{ "one": "two", "buckle": ["my", "shoe"] }')
{u'buckle': [u'my', u'shoe'], u'one': u'two'}

因此,您需要在Python文档中查找json

https://docs.python.org/3/library/json.html

如果您有字符串并将其转换为Python字典,请使用json.loads(string_variable)

要将Python字典转换为字符串,请使用json.dumps(dictionary)

暂无
暂无

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

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