Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
256 views
Welcome To Ask or Share your Answers For Others

1 Answer

$jsonres is actually an array.

This is because true is being passed as the second parameter to json_decode. If you, in fact, do want $jsonres to be an object, then just use json_decode($jsonObject);.

An easy way to check what your variable contains is to use the var_dump function.

$jsonres = json_decode($jsonObject);
var_dump($jsonres);

Also, be sure you have error_reporting turned on and set to E_ALL. The following code, $jsonres->Data, should be causing PHP to emit a "PHP Notice".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...