2016-09-23 60 views
-1

我正在使用第三方api將城市列表放入我的下拉列表中。我正在以這種形式如何打印JSON結果,當它有不同形式的數據使用PHP?

"TopDestination":"14621

</cityId>Amsterdam

</cityName>NL

</countryCode>Netherlands

</countryName>AMS

</cityCode>

我的回答是一個變量,名爲$水庫,當我試圖打印這樣

$res->TopDestination->cityId; 

但它不打印任何第三方API響應。如果我嘗試打印

$res->TopDestination; 

它的打印效果這樣

14621AmsterdamNLNetherlandsAMS

我應該用什麼來打印城市ID?

我從API響應是這樣

{"Error":{"ErrorCode":0,"ErrorMessage":""},"Status":1,"TokenId":"59831ca1-37e8-42fd-8350-0b3699b86505","TopDestination":"14621<\/cityId>Amsterdam<\/cityName>NL<\/countryCode>Netherlands<\/countryName>AMS<\/cityCode><\/City>17249<\/cityId>Antalya<\/cityName>TR<\/countryCode>Turkey<\/countryName>ANTA<\/cityCode><\/City>9434<\/cityId>Athens<\/cityName>GR<\/countryCode>Greece<\/countryName>ATH<\/cityCode><\/City>10142<\/cityId>Bali<\/cityName>ID<\/countryCode>Indonesia<\/countryName>ABAL<\/cityCode><\/City>16974<\/cityId>Bangkok<\/cityName>TH<\/countryCode>Thailand<\/countryName>BKK<\/cityCode><\/City>3518<\/cityId>Barcelona<\/cityName>ES<\/countryCode>Spain<\/countryName>BCN<\/cityCode><\/City>23131<\/cityId>Basel<\/cityName>CH<\/countryCode>Switzerland<\/countryName>BSL<\/cityCode><\/City>23884<\/cityId>Beijing<\/cityName>CN<\/countryCode>China<\/countryName>PEK<\/cityCode><\/City>13551<\/cityId>Beirut<\/cityName>LB<\/countryCode>Lebanon<\/countryName>BEY<\/cityCode><\/City>1996<\/cityId>Berlin<\/cityName>DE<\/countryCode>Germany<\/countryName>BER<\/cityCode><\/City>24602<\/cityId>Brussels<\/cityName>BE<\/countryCode>Belgium<\/countryName>BRU<\/cityCode><\/City>10085<\/cityId>Budapest<\/cityName>HU<\/countryCode>Hungary<\/countryName>BUD<\/cityCode><\/City>2418<\/cityId>Cairo<\/cityName>EG<\/countryCode>Egypt<\/countryName>CAI<\/cityCode><\/City>32443<\/cityId>Cape Town<\/cityName>ZA<\/countryCode>South Africa<\/countryName>CPT<\/cityCode><\/City>20491<\/cityId>Chicago<\/cityName>US<\/countryCode>United States<\/countryName>CHI<\/cityCode><\/City>125<\/cityId>Cologne<\/cityName>DE<\/countryCode>Germany<\/countryName>CGN<\/cityCode><\/City>13616<\/cityId>Colombo<\/cityName>LK<\/countryCode>Sri Lanka<\/countryName>CMB<\/cityCode><\/City>2142<\/cityId>Copenhagen<\/cityName>DK<\/countryCode>Denmark<\/countryName>CPH<\/cityCode><\/City>25921<\/cityId>Dubai<\/cityName>AE<\/countryCode>United Arab Emirates<\/countryName>DXB<\/cityCode><\/City>32515<\/cityId>Durban<\/cityName>ZA<\/countryCode>South Africa<\/countryName>DUR<\/cityCode><\/City>27352<\/cityId>Dusseldorf<\/cityName>DE<\/countryCode>Germany<\/countryName>DUS<\/cityCode><\/City>8030<\/cityId>Edinburgh<\/cityName>GB<\/countryCode>United Kingdom<\/countryName>EDI<\/cityCode><\/City>11213<\/cityId>Florence<\/cityName>IT<\/countryCode>Italy<\/countryName>FLR<\/cityCode><\/City>22753<\/cityId>Frankfurt<\/cityName>DE<\/countryCode>Germany<\/countryName>FRA<\/cityCode><\/City>23435<\/cityId>Geneva<\/cityName>CH<\/countryCode>Switzerland<\/countryName>GVA<\/cityCode><\/City>26698<\/cityId>Gold Coast<\/cityName>AU<\/countryCode>Australia<\/countryName>OOL<\/cityCode><\/City>23960<\/cityId>Guangzhou<\/cityName>CN<\/countryCode>China<\/countryName>CAN<\/cityCode><\/City>379<\/cityId>Hamburg<\/cityName>DE<\/countryCode>Germany<\/countryName>HAM<\/cityCode><\/City>9926<\/cityId>Hong Kong<\/cityName>HK<\/countryCode>Hong Kong<\/countryName>HKG<\/cityCode><\/City>26996<\/cityId>Innsbruck<\/cityName>AT<\/countryCode>Austria<\/countryName>INN<\/cityCode><\/City>23037<\/cityId>Interlaken<\/cityName>CH<\/countryCode>Switzerland<\/countryName>INTE<\/cityCode><\/City>17306<\/cityId>Istanbul<\/cityName>TR<\/countryCode>Turkey<\/countryName>IST<\/cityCode><\/City>32569<\/cityId>Johannesburg<\/cityName>ZA<\/countryCode>South Africa<\/countryName>JNB<\/cityCode><\/City>15054<\/cityId>Kathmandu<\/cityName>NP<\/countryCode>Nepal<\/countryName>KTM<\/cityCode><\/City>14386<\/cityId>Kuala Lumpur<\/cityName>MY<\/countryCode>Malaysia<\/countryName>KUL<\/cityCode><\/City>35827<\/cityId>Kuwait<\/cityName>KW<\/countryCode>Kuwait<\/countryName>KWI<\/cityCode><\/City>14285<\/cityId>Langkawi<\/cityName>MY<\/countryCode>Malaysia<\/countryName>LANK<\/cityCode><\/City>32827<\/cityId>Las Vegas<\/cityName>US<\/countryCode>United States<\/countryName>LAS<\/cityCode><\/City>9245<\/cityId>London<\/cityName>GB<\/countryCode>United Kingdom<\/countryName>LON<\/cityCode><\/City>34549<\/cityId>London<\/cityName>CA<\/countryCode>Canada<\/countryName>YXU<\/cityCode><\/City>18631<\/cityId>Los Angeles<\/cityName>US<\/countryCode>United States<\/countryName>LAX<\/cityCode><\/City>23273<\/cityId>Lucerne<\/cityName>CH<\/countryCode>Switzerland<\/countryName>LUCE<\/cityCode><\/City>13674<\/cityId>Macau<\/cityName>MO<\/countryCode>Macau<\/countryName>MFM<\/cityCode><\/City>3000<\/cityId>Madrid<\/cityName>ES<\/countryCode>Spain<\/countryName>MAD<\/cityCode><\/City>14254<\/cityId>Mauritius<\/cityName>MU<\/countryCode>Mauritius<\/countryName>MRUW<\/cityCode><\/City>26916<\/cityId>Melbourne<\/cityName>AU<\/countryCode>Australia<\/countryName>MEL<\/cityCode><\/City>11297<\/cityId>Milan<\/cityName>IT<\/countryCode>Italy<\/countryName>MIL<\/cityCode><\/City>266<\/cityId>Munich<\/cityName>DE<\/countryCode>Germany<\/countryName>MUC<\/cityCode><\/City>13443<\/cityId>Nairobi<\/cityName>KE<\/countryCode>Kenya<\/countryName>NBO<\/cityCode><\/City>28985<\/cityId>New York<\/cityName>US<\/countryCode>United States<\/countryName>NYC<\/cityCode><\/City>4419<\/cityId>Nice<\/cityName>FR<\/countryCode>France<\/countryName>NCE<\/cityCode><\/City>19533<\/cityId>Orlando<\/cityName>US<\/countryCode>United States<\/countryName>ORL<\/cityCode><\/City>4775<\/cityId>Paris<\/cityName>FR<\/countryCode>France<\/countryName>PAR<\/cityCode><\/City>36773<\/cityId>Pattaya Beach<\/cityName>TH<\/countryCode>Thailand<\/countryName>PATT<\/cityCode><\/City>14321<\/cityId>Penang<\/cityName>MY<\/countryCode>Malaysia<\/countryName>PEN<\/cityCode><\/City>17093<\/cityId>Phuket<\/cityName>TH<\/countryCode>Thailand<\/countryName>PHUW<\/cityCode><\/City>24332<\/cityId>Prague<\/cityName>CZ<\/countryCode>Czech Republic<\/countryName>PRG<\/cityCode><\/City>12771<\/cityId>Rome<\/cityName>IT<\/countryCode>Italy<\/countryName>ROM<\/cityCode><\/City>27022<\/cityId>Salzburg<\/cityName>AT<\/countryCode>Austria<\/countryName>SZG<\/cityCode><\/City>18857<\/cityId>San Diego<\/cityName>US<\/countryCode>United States<\/countryName>SAN<\/cityCode><\/City>18861<\/cityId>San Francisco<\/cityName>US<\/countryCode>United States<\/countryName>SFO<\/cityCode><\/City>23909<\/cityId>Shanghai<\/cityName>CN<\/countryCode>China<\/countryName>SHA<\/cityCode><\/City>23976<\/cityId>Shenzhen<\/cityName>CN<\/countryCode>China<\/countryName>SHEN<\/cityCode><\/City>16532<\/cityId>Singapore<\/cityName>SG<\/countryCode>Singapore<\/countryName>SIN<\/cityCode><\/City>34326<\/cityId>Sydney<\/cityName>AU<\/countryCode>Australia<\/countryName>SYD<\/cityCode><\/City>13420<\/cityId>Tokyo<\/cityName>JP<\/countryCode>Japan<\/countryName>TYO<\/cityCode><\/City>34661<\/cityId>Toronto<\/cityName>CA<\/countryCode>Canada<\/countryName>YTO<\/cityCode><\/City>25561<\/cityId>Vancouver<\/cityName>CA<\/countryCode>Canada<\/countryName>YVR<\/cityCode><\/City>11501<\/cityId>Venice<\/cityName>IT<\/countryCode>Italy<\/countryName>VCE<\/cityCode><\/City>27059<\/cityId>Vienna<\/cityName>AT<\/countryCode>Austria<\/countryName>VIE<\/cityCode><\/City>23418<\/cityId>Zermatt<\/cityName>CH<\/countryCode>Switzerland<\/countryName>ZERM<\/cityCode><\/City>23424<\/cityId>Zurich<\/cityName>CH<\/countryCode>Switzerland<\/countryName>ZRH<\/cityCode><\/City><\/Cities>"} 
+1

你能提供來自第三方的API完全反應? –

+1

請將您所擁有的JSON結構的相關部分添加到問題中。 – arkascha

+0

我已添加,請看看 –

回答

1

的問題是從API JSON響應。你的數組$ res ['TopDestination']只包含一個字符串,因此你不能訪問「cityId」。您必須處理$ res ['TopDestination']。

根據您的JSON這應該工作(它是測試!):

<?php 
// $json = result from API (a valid JSON string!) 
$res=json_decode($json, true); 

// get TopDestination string into an array 
$data=explode("</City>", $res["TopDestination"]); 
array_pop($data); 
// create a new Database of cities 
$cities=array(); 

foreach ($data as $d) { 
    $tmp=explode("</cityId>",$d); 
    $city_id=$tmp[0]; 
    $tmp=explode("</cityName>", $tmp[1]); 
    $city_name=$tmp[0]; 
    $tmp=explode("</countryCode>", $tmp[1]); 
    $country_code=$tmp[0]; 
    $tmp=explode("</countryName>", $tmp[1]); 
    $country_name=$tmp[0]; 
    $city_code=$tmp[1]; 

    // add the city info in the database 
    $cities[]=array(
    "cityId" => $city_id, 
    "cityName" => $city_name, 
    "cityCode" => $city_code, 
    "countryCode" => $country_code, 
    "countryName" => $country_name, 
); 
} 

// print the database array to check it 
echo "<pre>"; 
print_r($cities); 
echo "</pre>"; 

?> 

結果... :)

Array 
(
[0] => Array 
    (
     [cityId] => 14621 
     [cityName] => Amsterdam 
     [cityCode] => AMS 
     [countryCode] => NL 
     [countryName] => Netherlands 
    ) 

[1] => Array 
    (
     [cityId] => 17249 
     [cityName] => Antalya 
     [cityCode] => ANTA 
     [countryCode] => TR 
     [countryName] => Turkey 
    ) 

[2] => Array 
    (
     [cityId] => 9434 
     [cityName] => Athens 
     [cityCode] => ATH 
     [countryCode] => GR 
     [countryName] => Greece 
    ) 

[3] => Array 
    (
... 
+0

它顯示此錯誤「致命錯誤:不能使用類型爲stdClass的對象作爲數組」 –

+0

您必須小心您放入json_decode函數中的內容。在我的代碼中,我把JSON字符串放在$ json中(它必須是一個字符串!)。我對該帖子進行了編輯,以解釋$ json和$ res在我的代碼中。 – verjas

+0

是的,我也已經把正確的JSON字符串放在json_decode中,我從捲曲響應中得到 –

相關問題