[英]Read Json Object Using PHP
I am trying to read a json object using php as follows 我正在尝试使用php读取json对象,如下所示
$jsonObject = file_get_contents("http://10.12.12.189:9080/NonMotorServices/CommonServices.svc/FetchCurrency");
$jsonres = json_decode($jsonObject, true);
Following are the content of the object 以下是对象的内容
{"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}
I need to list down currency in a html selection and i used following to do so. 我需要在html选择中列出货币,我用下面的方法列出了货币。
echo '<select>';
foreach($jsonres->Data as $option)
{ echo '<option value=' . $option->CurrencyDescription . '>' . $option->CurrencyDescription . '</option>';
}
echo '</select>';
I am getting an empty selection as a result and i need to load 'CurrencyDescription ' as option value. 结果我得到一个空选择,我需要加载'CurrencyDescription'作为选项值。 Please help me with this.
请帮我解决一下这个。 and please explain what is the error i made because i am new to php and json.
并请解释我犯了什么错误,因为我是php和json的新手。
Full Code as follows 完整代码如下
<?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
is actually an array. $jsonres
实际上是一个数组。
This is because true
is being passed as the second parameter to json_decode
. 这是因为将
true
作为第二个参数传递给json_decode
。 If you, in fact, do want $jsonres
to be an object, then just use json_decode($jsonObject);
实际上,如果您确实希望
$jsonres
成为对象,则只需使用json_decode($jsonObject);
. 。
An easy way to check what your variable contains is to use the var_dump
function. 一种简单的检查变量包含内容的方法是使用
var_dump
函数。
$jsonres = json_decode($jsonObject);
var_dump($jsonres);
Also, be sure you have error_reporting turned on and set to E_ALL. 另外,请确保已打开error_reporting并将其设置为E_ALL。 The following code,
$jsonres->Data
, should be causing PHP to emit a "PHP Notice". 以下代码
$jsonres->Data
应当使PHP发出“ PHP通知”。
Finally I Solve The Problem. 最后,我解决了问题。 Here is My Solution
这是我的解决方案
<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.