【发布时间】: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 还是我的脚本,以及在这两种情况下要改变什么?
【问题讨论】:
-
AFAIK 不是有效的 JSON,值周围缺少引号。
-
我正在处理完全相同的问题。我有一个问题要问线程作者:你如何访问@json 变量?我一直认为@json 是内存中的 HASH,但我无法访问变量或循环遍历它。