Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

Read Json Object Using PHP

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

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

1 Reply

0 votes
by (71.8m points)

$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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...