格式错误的JSON字符串,既不是数组,也不是对象,数字,字符串或原子

Max*_*Max 7 perl parsing json

我用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
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

这里是脚本:
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)};
Run Code Online (Sandbox Code Playgroud)

最后我得到的错误:
错误

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

问:有人可以告诉我问题是来自json还是我的脚本以及在任何一种情况下要改变什么?

仅供参考,json来自Alfresco审计api.

Que*_*tin 10

这个:

"application":AuditExampleLogin1,
Run Code Online (Sandbox Code Playgroud)

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

如果是字符串,则需要将其括起来".

另请参见:JSON Lint.


Bil*_*ert 3

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

my @json = @{decode_json($result)};
Run Code Online (Sandbox Code Playgroud)

需要公正

my @json = decode_json($result);
Run Code Online (Sandbox Code Playgroud)

  • 您要求 JSON 解析器成功解析无效 JSON 的数据。那肯定是疯狂的:) (7认同)