[英]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.