字符串似乎是有效的JSON,但是`json_decode()`返回NULL [英] String appears to be valid JSON, but `json_decode()` returns NULL

查看:131
本文介绍了字符串似乎是有效的JSON,但是`json_decode()`返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经找到了错误所在:

I think I've found where the error lies:

    $convertJSON = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=" . $currencyValue . $currencySelectValue . "%3D%3FUSD", true);
    var_dump($convertJSON);
    $convertArr = json_decode($convertJSON, true);
    var_dump($convertArr);

我这样做是为了调试,我得到了这个结果(我输入了555和欧元):

I do that to debug, and I get this result (I entered 555 and Euros):

string(68) "{lhs: "555 Euros",rhs: "796.64700 U.S. dollars",error: "",icc: true}"
NULL

因此看来,用于解码JSON对象的PHP函数在某处做错了.有帮助吗?

So it seems that the PHP function to decode the JSON object is doing something wrong somewhere. Any help?

推荐答案

Google给您的响应不是有效的JSON,因为未引用标签.您必须自己解析.

The response Google is giving you isn't valid JSON because the labels are not quoted. You'll have to parse it yourself.

$response = '{lhs: "555 Euros",rhs: "796.64700 U.S. dollars",error: "",icc: true';
preg_match('/rhs:\s*"([^"]+)"/', $response, $m);
echo $m[1];

输出:

796.64700 U.S. dollars

这篇关于字符串似乎是有效的JSON,但是`json_decode()`返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆