【问题标题】:malformed JSON string, neither array, object, number, string or atom格式错误的 JSON 字符串,既不是数组、对象、数字、字符串或原子
【发布时间】:2012-01-03 22:40:00
【问题描述】:

我在使用 perl 解析 json 对象时遇到问题,我不知道问题是来自 json 还是我的脚本。这是json:

test.json

{
   "count":3,
   "entries": 
   [
      {
         "id":85,
         "application":AuditExampleLogin1,
         "user":admin,
         "time":"2011-11-22T10:29:37.422Z",
         "values":
null
      },
      {
         "id":87,
         "application":AuditExampleLogin1,
         "user":admin,
         "time":"2011-11-22T10:30:56.235Z",
         "values":
null
      },
      {
         "id":89,
         "application":AuditExampleLogin1,
         "user":admin,
         "time":"2011-11-22T10:33:15.000Z",
         "values":
null
      }
   ]
}

这里是脚本:
script.pl

#!/usr/bin/perl -w
use strict;
use JSON;
open FILE, 'test.json' or die "Could not open file inputfile: $!";
sysread(FILE, my $result, -s FILE);
close FILE or die "Could not close file: $!"; 
my @json = @{decode_json($result)};

最后是我得到的错误:
错误

malformed JSON string, neither array, object, number, string or atom,
at character offset 86 (before "AuditExampleLogin1,\n...") at     
./script.pl line 7.

问:谁能告诉我这个问题是来自 json 还是我的脚本,以及在这两种情况下要改变什么?

仅供参考json is coming from Alfresco auditing api

【问题讨论】:

  • AFAIK 不是有效的 JSON,值周围缺少引号。
  • 我正在处理完全相同的问题。我有一个问题要问线程作者:你如何访问@json 变量?我一直认为@json 是内存中的 HASH,但我无法访问变量或循环遍历它。

标签: json perl parsing


【解决方案1】:

这个:

"application":AuditExampleLogin1,

… 是无效的 JSON。 AuditExampleLogin1 不是数组、对象、数字、字符串或原子。我不知道它应该是什么,所以我不能告诉你要改成什么。

如果是字符串,则需要用"括起来。

另请参阅:JSON Lint

【讨论】:

    【解决方案2】:

    如果引用了 Audit... 和 admin 值,它会起作用。线

    my @json = @{decode_json($result)};
    

    需要只是

    my @json = decode_json($result);
    

    【讨论】:

    • 确实在引用 applicationuser 值时有效。有没有办法告诉perl 在解析 JSON 时更加宽松并接受未引用的字符串? (这将使我不必重写 json)。
    • 不确定,JSON 模块作者认为严格检查是一项安全功能。你可能会戳代码,JSON::PP 是纯 perl 版本。
    • 您要求 JSON 解析器成功解析不是有效 JSON 的数据。那肯定是疯狂的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2021-09-25
    • 2019-11-21
    • 1970-01-01
    • 2021-03-28
    • 2014-04-24
    • 2017-08-21
    相关资源
    最近更新 更多