簡體   English   中英

使用PHP讀取Json對象

[英]Read Json Object Using PHP

我正在嘗試使用php讀取json對象,如下所示

$jsonObject = file_get_contents("http://10.12.12.189:9080/NonMotorServices/CommonServices.svc/FetchCurrency");      
        $jsonres = json_decode($jsonObject, true);

以下是對象的內容

{"Data":"[{\"CurrencySymbol\":\"AU$\",\"CurrencyDescription\":\"Austrailian Dollar\",\"CurrencyRate\":135.42,\"CurrencyType\":\"AUD\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":135.42},{\"CurrencySymbol\":\"£.\",\"CurrencyDescription\":\"British pound sterling\",\"CurrencyRate\":212.62,\"CurrencyType\":\"GBP\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":212.62},{\"CurrencySymbol\":\"EURO\",\"CurrencyDescription\":\"Euro\",\"CurrencyRate\":171.2,\"CurrencyType\":\"EUR\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":171.2},{\"CurrencySymbol\":\"¥.\",\"CurrencyDescription\":\"Japanese yen\",\"CurrencyRate\":1.6809,\"CurrencyType\":\"JPY\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":1.6809},{\"CurrencySymbol\":\"SIN$\",\"CurrencyDescription\":\"Singapore Dollar\",\"CurrencyRate\":107.3,\"CurrencyType\":\"SGD\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":107.3},{\"CurrencySymbol\":\"Rs.\",\"CurrencyDescription\":\"Sri Lankan Rupees\",\"CurrencyRate\":1,\"CurrencyType\":\"LKR\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":1},{\"CurrencySymbol\":\"CHF\",\"CurrencyDescription\":\"Swiss Frank\",\"CurrencyRate\":141.71,\"CurrencyType\":\"CHF\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":141.71},{\"CurrencySymbol\":\"US$.\",\"CurrencyDescription\":\"United States dollar\",\"CurrencyRate\":135,\"CurrencyType\":\"USD\",\"RequestDate\":\"\\\/Date(1408041000000)\\\/\",\"PolicyId\":\"\",\"QuotationId\":0,\"SellingRate\":137}]","ID":1}

我需要在html選擇中列出貨幣,我用下面的方法列出了貨幣。

echo '<select>';
foreach($jsonres->Data as $option)
    {    echo '<option value=' . $option->CurrencyDescription . '>' . $option->CurrencyDescription . '</option>';  
}
echo '</select>'; 

結果我得到一個空選擇,我需要加載'CurrencyDescription'作為選項值。 請幫我解決一下這個。 並請解釋我犯了什么錯誤,因為我是php和json的新手。

完整代碼如下

     <?php

        $jsonObject = file_get_contents("http://10.12.12.189:9080/NonMotorServices/CommonServices.svc/FetchCurrency");      
        $jsonres = json_decode($jsonObject, true);

 echo '<select>';
foreach($jsonres->Data as $option)
    {    echo '<option value=' . $option->CurrencyDescription . '>' . $option->CurrencyDescription . '</option>';  
}
echo '</select>';


           ?>

$jsonres實際上是一個數組。

這是因為將true作為第二個參數傳遞給json_decode 實際上,如果您確實希望$jsonres成為對象,則只需使用json_decode($jsonObject);

一種簡單的檢查變量包含內容的方法是使用var_dump函數。

$jsonres = json_decode($jsonObject);
var_dump($jsonres);

另外,請確保已打開error_reporting並將其設置為E_ALL。 以下代碼$jsonres->Data應當使PHP發出“ PHP通知”。

最后,我解決了問題。 這是我的解決方案

 <select>    
         <?php
        $jsonObject = file_get_contents("http://10.12.12.189:9080/NonMotorServices/CommonServices.svc/FetchCurrency");
    $jsonres = json_decode($jsonObject,true);               
        $val = $jsonres['Data'];      
$phpArray = json_decode($val, true);
foreach ($phpArray as $key => $value) {
    $curName;
    foreach ($value as $k => $v) { 
        if($k === 'CurrencyDescription'){ 
        $curName=$v;}

    }
    echo '<option value=' . $curName. '>' . $curName. '</option>'; 
}

           ?>
    </select>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM