2011-12-24 48 views
0

我正在使用Google的货币计算api。它返回json。我可以将URL内容转换为一个字符串,但我怎样才能解析这个在PHP?我正在尝试返回“rhs”值。在php中解码json

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

[让我谷歌认为你(http://tinyurl.com/795fd6h ) – kapa 2011-12-24 00:20:44

回答

0

使用json_decode

$rhs = json_decode($string); 
// $rhs is an object now. 
+2

不,没有通过TRUE作为第二个参数,$ rhs是一个对象现在,不是一个字符串。 – Interrobang 2011-12-24 00:11:00

+0

哦,所以它!对不起,错误信息 – Indranil 2011-12-24 00:15:42

0

没有太多吧:

$obj = json_decode($string); 
$rhs = $obj->rhs; 
1

json_decode()是你会用什么。

$data = json_decode($string); 
$rhs = $data->rhs; 
-1

使用PHP的json_decode()功能:

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

echo $rhs; 
+2

与Indranil类似,使用'json_decode()'而不使用第二个参数为真意味着'$ json_array'类型为Object,而不是Array类型。您必须使用' - >'语法访问对象的成员。 – Interrobang 2011-12-24 00:18:43

+0

良好的电话@Interrobang。固定。 – 2011-12-24 00:26:08