使用PHP读取Json对象

时间:2014-08-15 03:46:32

标签: php json

我正在尝试使用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>'; 

我得到一个空的选择结果,我需要加载&#39; CurrencyDescription&#39;作为期权价值。请帮我解决一下这个。请解释我犯的错误是什么,因为我是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>';


           ?>

2 个答案:

答案 0 :(得分:2)

$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 Notice”。

答案 1 :(得分:0)

最后我解决了这个问题。这是我的解决方案

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