-1

I'm using Google's currency calculation api. It returns json. I can get the URL contents into a string, but how can I parse this in php? I'm trying to return the "rhs" value.

    $string='{lhs: "1 Euro",rhs: "1.3067 U.S. dollars",error: "",icc: true}'; 
    $rhs=?????
    echo $rhs; 

5 Answers5

1

json_decode() is what you would use.

$data = json_decode($string);
$rhs = $data->rhs;
alex
  • 479,566
  • 201
  • 878
  • 984
0

use json_decode

$rhs = json_decode($string);
// $rhs is an object now.
Andrew Odri
  • 8,868
  • 5
  • 46
  • 55
Indranil
  • 2,451
  • 1
  • 23
  • 31
0

There's not much to it:

$obj = json_decode($string);
$rhs = $obj->rhs;
WWW
  • 9,734
  • 1
  • 29
  • 33
0
$string='{lhs: "1 Euro",rhs: "1.3067 U.S. dollars",error: "",icc: true}'; 
$json=json_decode($string);
echo $json->rhs;

http://us.php.net/json_decode

Interrobang
  • 16,984
  • 3
  • 55
  • 63
-1

Use PHP's json_decode() function:

$string      = '{lhs: "1 Euro",rhs: "1.3067 U.S. dollars",error: "",icc: true}';
$json_object = json_decode($string);
$rhs         = $json_array->rhs;

echo $rhs; 
Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
  • 2
    Similar to Indranil, using `json_decode()` without the second parameter being true means that `$json_array` is of type Object, not of type Array. You must access members of an Object with `->` syntax. – Interrobang Dec 24 '11 at 00:18