Php json_解码无法处理字符串变量

Php json_解码无法处理字符串变量,php,arrays,json,string,decode,Php,Arrays,Json,String,Decode,我试图将一个json字符串解码到php数组中,当我使用json_last_error()时,我得到了一个语法错误(4) 该字符串来自对Infusionsoft数据库的API调用。数据库返回一个包含一个键/值对的数组。该值包含json字符串 Array ( [_PMSChargeItems] => [{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":

我试图将一个json字符串解码到php数组中,当我使用json_last_error()时,我得到了一个语法错误(4)

该字符串来自对Infusionsoft数据库的API调用。数据库返回一个包含一个键/值对的数组。该值包含json字符串

Array ( [_PMSChargeItems] => [{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}] )
当我将值保存到字符串变量并将其传递给json_decode()时,我得到了语法错误(它不起作用)。如果我将整个json字符串本身(通过回显字符串var值进行复制和粘贴)传递给json_decode(),则会创建数组(它可以工作)

我已经在和上验证了json字符串

当使用变量将字符串传递给json_decode函数时,如何正确解码该字符串

下面是我用来解码的代码:

$cid = $_GET['Id'];
$returnFields = array('_PMSChargeItems');
$conDat = $appConnect->dsLoad("Contact", $cid, $returnFields);

$a = $conDat['_PMSChargeItems'];
var_dump(json_decode($a, true));

echo json_last_error();
下面是解码前var_dump($a)的结果:

string(4649) "[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}]"
我尝试使用html_实体和html_实体解码查找隐藏字符,但没有找到任何隐藏字符。我还使用mb_detect_编码确认字符串包含所有ASCII字符

我只是尝试了以下方法:

var_dump(json_decode(strval($a), true));
结果为空,解码错误为语法错误

奇怪的是,$a上的var_dump给了我4649个字符计数,而字符串本身的strlen(“[(“Date”…)给了2789个字符计数…

EDIT

如果在非json编码的数组上调用json_decode,则需要在json_编码的字符串上调用json_decode,即$a的元素“_pmschargeims”

这似乎有效:

$a = '[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}]';
print_r( json_decode( $a ) );
或数组方式:

$a = array( '_PMSChargeItems' => '[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}]' );
print_r( json_decode( $a['_PMSChargeItems'] ) );

你似乎把JavaScript和PHP弄混了

多亏了你的帮助,我解决了这个难题

当从Infusionsoft数据库返回JSON字符串时,所有引号都存储为$quot;,而不是双引号

我通过将字符串保存到一个.txt文件(使用fopen、fwrite和fclose)并用文本编辑器打开该文件发现了这一点。我一直认为我需要用十六进制编辑器查看该文件(正如Ryan建议的那样-我想这是他的名字,他删除了包含该建议的评论,所以我不确定)但事实证明这是不必要的。以下是文件中的数据:

[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},....
为了删除引号,我在字符串上使用了htmlspecialchars_decode(),然后成功地将字符串传递到json_decode。下面是代码:

$a = $conDat['_PMSChargeItems'];
$b = htmlspecialchars_decode($a);
$c = json_decode($b);

再次感谢您的宝贵意见和回答!

向我们展示您实际调用json的代码。您所指的函数是一个允许您访问Infusionsoft数据库中字段的函数。我刚刚在描述中添加了print_r($a);的结果。感谢您的帮助!刚刚将print_r更改为var_dump(我在解码和更早的时候更改了它)。谢谢你的建议!这对其他人更有帮助。我只是自己尝试了一下,但我无法复制你的错误,对我来说效果很好。你能证明它是编码的吗?也许那里发生了什么事。或者你的PHP版本有什么事。解决了。非常感谢@AndrewB和另一个人(我想是Ryan)谢谢你的帮助。我会尽快发布解决方案。我认为我没有给你足够的信息(如果是这样的话,我很抱歉)。我只是在描述中添加了用于json_解码的代码。我将数组值存储在一个变量中,并将该变量传递给json_decode()。