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

时间:2011-11-22 14:42:57

标签: json perl parsing

我在使用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

2 个答案:

答案 0 :(得分:10)

此:

"application":AuditExampleLogin1,

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

如果是字符串,则需要将其括在"

另请参阅:JSON Lint

答案 1 :(得分:2)

如果引用了Audit ...和admin值,它就有效。这条线

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

需要只是

my @json = decode_json($result);