Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP读取Json对象_Php_Json - Fatal编程技术网

使用PHP读取Json对象

使用PHP读取Json对象,php,json,Php,Json,我尝试使用php读取json对象,如下所示 $jsonObject = file_get_contents("http://10.12.12.189:9080/NonMotorServices/CommonServices.svc/FetchCurrency"); $jsonres = json_decode($jsonObject, true); <?php $jsonObject = file_get_contents("htt

我尝试使用php读取json对象,如下所示

$jsonObject = file_get_contents("http://10.12.12.189:9080/NonMotorServices/CommonServices.svc/FetchCurrency");      
        $jsonres = json_decode($jsonObject, true);
     <?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>';


           ?>
以下是对象的内容

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

完整代码如下

$jsonObject = file_get_contents("http://10.12.12.189:9080/NonMotorServices/CommonServices.svc/FetchCurrency");      
        $jsonres = json_decode($jsonObject, true);
     <?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);

此外,请确保已打开并设置为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>