I am trying to read a json object using php as follows
$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.
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. Please help me with this. and please explain what is the error i made because i am new to php and 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>';
?>
See Question&Answers more detail:
os