簡體   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