当前位置: 首页 > 知识库问答 >
问题:

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

全鸿晖
2023-03-14

我试图将一个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字符串http://jsonlint.com/和http://www.functions-online.com/json_decode.html.

当使用变量将字符串传递给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(“[(“日期”)给了我4649个字符字符串本身的长度给出了2789个字符的计数。。。

共有2个答案

毛峻
2023-03-14

编辑

如果在非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

湛财
2023-03-14

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

当从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);

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

 类似资料:
  • Bash 支持的字符串操作数量达到了一个惊人的数目。但可惜的是,这些操作工具缺乏一个统一的核心。他们中的一些是参数代换的子集,另外一些则是 UNIX 下 expr 函数的子集。这将会导致语法前后不一致或者功能上出现重叠,更不用说那些可能导致的混乱了。 字符串长度 ${#string} expr length $string 上面两个表达式等价于C语言中的 strlen() 函数。 expr "$s

  • 问题内容: 我有这个工作代码来打印字符串排列而没有重复,但是我无法理解它在逻辑上是如何工作的。任何建议都会非常有帮助。 函数调用: 问题答案: 上面的for循环正在做魔术 输入ABCD 迭代 输入:BCD沙发:A ....递归继续 输入:ACD沙发:B .... 输入:ABD沙发:C .... 输入:ABC沙发:D ..... 希望这可以帮助

  • 我从数据库中选择一些数据,并将其编码为json,但我对捷克标志有问题,例如 á,í,ř,č,ž... 我的文件是utf-8编码的,我的数据库也是utf-8编码的,我已经将头设置为utf-8编码。我还能做什么? 我的代码: 和部分打印的json: 编辑:如果没有mb_convert_encoding()函数,打印的字符串是空的,并且打印“错误”。

  • StringBuffer 是一个字符串拼接工具,和java中的StringBuilder类似。对于那些需要大量的字符串连接的时候,用 StringBuffer 更高效一些。它实现了以下API: class StringBuffer { public function __construct($str); public function isEmpty(); publi

  • 字符串在我们平常的Web开发中经常用到,包括用户的输入,数据库读取的数据等,我们经常需要对字符串进行分割、连接、转换等操作,本小节将通过Go标准库中的strings和strconv两个包中的函数来讲解如何进行有效快速的操作。 字符串操作 下面这些函数来自于strings包,这里介绍一些我平常经常用到的函数,更详细的请参考官方的文档。 func Contains(s, substr string)

  • 函数 说明 Series.str.capitalize() 将 Series / 索引中的字符串转换为大写。 Series.str.cat([others, sep, na_rep, join]) 使用给定的分隔符连接 Series / 索引中的字符串。 Series.str.center(width[, fillchar]) 用附加字符填充 Series / 索引中字符串的左侧和右侧。 Seri