我在Python中使用来自Web请求的JSON输出,但是我很难将这些数据转换为Python字典。
如果你在这里查看get属性模型: 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:期望的字符串或缓冲区
答案 0 :(得分:1)
semanticContentAttribute
应该将JSON字符串转换为字典。
您首先需要json.loads
。有关详细信息,请参阅https://docs.python.org/2/library/json.html。
import json
答案 1 :(得分:1)
所以你需要在Python文档中查找json
。
https://docs.python.org/3/library/json.html
如果您有一个字符串并希望将其转换为Python字典,则使用json.loads(string_variable)
。
要将Python词典转换为字符串,请使用json.dumps(dictionary)
。