Python urllib2 返回JSON

2024-06-01 06:05:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从这个URL返回一个JSON对象。我正确地传递了我的API,但是当我去做的时候

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

我得到这个错误

^{pr2}$

这是我的全部代码

^{3}$

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>

Tags: jsonreaddataversionresponsestatuspagecode
1条回答
网友
1楼 · 发布于 2024-06-01 06:05:47

根据您提供的documentation链接,您省略了Accept头。在

There are two required headers:

  1. Accept - set to either
    • application/json
    • application/xml

如果没有这个头,它似乎默认为application/xml,并返回格式为xml而不是{}的信息

要返回json,只需将Accept设置为application/json

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

相关问题 更多 >