2017-09-22 83 views
0

我想從這個URL返回一個JSON對象。我正確地掠過我的API,但是當我去做python urllib2 return JSON

data=json.loads(a.read()) 
print data 

我得到這個錯誤

Traceback (most recent call last): 
    File "C:\Python27\ArcGIS10.1\lib\bdb.py", line 387, in run 
    exec cmd in globals, locals 
    File "<module1>", line 1, in <module> 
    from urllib2 import Request, urlopen 
    File "C:\Python27\ArcGIS10.1\lib\json\__init__.py", line 326, in loads 
    return _default_decoder.decode(s) 
    File "C:\Python27\ArcGIS10.1\lib\json\decoder.py", line 366, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "C:\Python27\ArcGIS10.1\lib\json\decoder.py", line 384, in raw_decode 
    raise ValueError("No JSON object could be decoded") 
ValueError: No JSON object could be decoded 

這裏是我的全部代碼

from urllib2 import Request, urlopen 
import urllib2 
import json 
apikey='abid' 
url='https://search.onboard-apis.com/propertyapi/v1.0.0/sale/detail?address1=586+FRANKLIN+AVE&address2=brooklyn+NY+11238' 
weburl=Request(url) 
weburl.add_header('apikey',apikey) 
a=urlopen(weburl) 
data=json.loads(a.read()) 
print data 

https://developer.onboard-apis.com/docs文檔

這個問題沒有幫助Python urllib2: Receive JSON response from url

這是a.read()打印

<Response> 
    <status> 
    <version>1.0.0</version> 
    <code>0</code> 
    <msg>SuccessWithResult</msg> 
    <total>1</total> 
    <page>1</page> 
    <pagesize>10</pagesize> 
    </status> 
    <property> 
    <identifier> 
     <obPropId>8831109036047</obPropId> 
     <fips>36047</fips> 
     <apn>011340051</apn> 
     <apnOrig>1134 51</apnOrig> 
    </identifier> 
    <lot> 
     <depth>80</depth> 
     <frontage>20</frontage> 
     <lotnum>51</lotnum> 
     <lotsize1>0.0367</lotsize1> 
     <lotsize2>1600</lotsize2> 
    </lot> 
    <area> 
     <blockNum>1134</blockNum> 
     <countrysecsubd>Kings County</countrysecsubd> 
     <countyuse1>B3</countyuse1> 
     <muncode>27</muncode> 
     <munname>CROWN HEIGHTS</munname> 
     <srvySection>04</srvySection> 
     <srvyTownship>07</srvyTownship> 
     <taxcodearea>1</taxcodearea> 
    </area> 
    <address> 
     <country>US</country> 
     <countrySubd>NY</countrySubd> 
     <line1>586 FRANKLIN AVE</line1> 
     <line2>BROOKLYN, NY 11238</line2> 
     <locality>Brooklyn</locality> 
     <matchCode>ExaStr</matchCode> 
     <oneLine>586 FRANKLIN AVE, BROOKLYN, NY 11238</oneLine> 
     <postal1>11238</postal1> 
     <postal2>3351</postal2> 
     <postal3>C008</postal3> 
    </address> 
    <location> 
     <accuracy>Street</accuracy> 
     <elevation>0</elevation> 
     <latitude>40.678325</latitude> 
     <longitude>-73.955376</longitude> 
     <distance>0</distance> 
     <geoid>CO36047,CS3610022,DB3600095,MT30004136,ND0000006436,ND0000499119,PL3651000,SB0000105657,SB0000106357,ZI11238</geoid> 
    </location> 
    <summary> 
     <absenteeInd>SITUS FROM SALE (ABSENTEE)</absenteeInd> 
     <propclass>"Duplex, Triplex, Quadplex)"</propclass> 
     <propsubtype>TWO FAMILY</propsubtype> 
     <proptype>DUPLEX</proptype> 
     <yearbuilt>0</yearbuilt> 
     <propLandUse>DUPLEX</propLandUse> 
     <propIndicator>21</propIndicator> 
    </summary> 
    <utilities /> 
    <building> 
     <size> 
     <bldgsize>0</bldgsize> 
     <grosssize>3200</grosssize> 
     <grosssizeadjusted>0</grosssizeadjusted> 
     <groundfloorsize>0</groundfloorsize> 
     <livingsize>0</livingsize> 
     <sizeInd>GROSS SQFT</sizeInd> 
     <universalsize>3200</universalsize> 
     </size> 
     <rooms> 
     <bathfixtures>0</bathfixtures> 
     <baths1qtr>0</baths1qtr> 
     <baths3qtr>0</baths3qtr> 
     <bathscalc>0</bathscalc> 
     <bathsfull>0</bathsfull> 
     <bathshalf>0</bathshalf> 
     <bathstotal>0</bathstotal> 
     <beds>0</beds> 
     <roomsTotal>0</roomsTotal> 
     </rooms> 
     <interior> 
     <bsmtsize>0</bsmtsize> 
     <fplccount>0</fplccount> 
     </interior> 
     <construction /> 
     <parking> 
     <prkgSize>0</prkgSize> 
     <prkgSpaces>0</prkgSpaces> 
     </parking> 
     <summary> 
     <bldgsNum>1</bldgsNum> 
     <bldgType>TWO FAMILY</bldgType> 
     <levels>3</levels> 
     <storyDesc>TWO FAMILY</storyDesc> 
     <unitsCount>4</unitsCount> 
     <yearbuilteffective>1910</yearbuilteffective> 
     </summary> 
    </building> 
    <vintage> 
     <lastModified>2016-5-24</lastModified> 
     <pubDate>2016-5-24</pubDate> 
    </vintage> 
    <sale> 
     <salesearchdate>2016-1-27</salesearchdate> 
     <saleTransDate>2016-1-27</saleTransDate> 
     <amount> 
     <saleamt>1650000</saleamt> 
     <salerecdate>2016-2-10</salerecdate> 
     <saledisclosuretype>0</saledisclosuretype> 
     <saledocnum>46760</saledocnum> 
     <saletranstype>Resale</saletranstype> 
     </amount> 
     <calculation> 
     <priceperbed>0</priceperbed> 
     <pricepersizeunit>516</pricepersizeunit> 
     </calculation> 
     <ownershiptransferpercentage>0</ownershiptransferpercentage> 
     <resaleornewconstruction>M</resaleornewconstruction> 
     <cashormortgagepurchase>M</cashormortgagepurchase> 
    </sale> 
    </property> 
</Response> 
+1

...但什麼數據是? –

+0

根據您鏈接到的文檔,您錯過了「Accept」標頭。就像伊格納西奧所說的那樣,知道'a.read()'實際上會給你帶來什麼是很好的。 (大概在那裏沒有JSON,並且可能有一個有用的錯誤信息。) – smarx

+0

例如,嘗試'raw_content = a.read();打印raw_content; data = json.loads(raw_content)...' – smarx

回答

0

根據您所提供的documentation鏈接,你離開了Accept頭。

有兩個必要標題:

  1. 欣然接受 - 設置爲
    • 應用/ JSON
    • 應用程序/ XML

沒有這個頭,這似乎是默認爲application/xml並返回格式化爲xml的信息,而不是json

要返回json,你只需要設置Acceptapplication/json

weburl.add_header('Accept', 'application/json')