格式错误的 JSON 字符串,既不是数组、对象、数字、字符串或原子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8228626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
malformed JSON string, neither array, object, number, string or atom
提问by Max
I have trouble parsing a json object with perl and I don't know if the issue is coming from the json or my script. Here is the 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
}
]
}
Here the script:
script.pl
这里的脚本:
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)};
And finally the error I'm getting:
error
最后是我得到的错误:
错误
malformed JSON string, neither array, object, number, string or atom,
at character offset 86 (before "AuditExampleLogin1,\n...") at
./script.pl line 7.
Q: Can someone tell me whether the issue is coming from the json or my script and what to change in either case?
问:有人能告诉我问题是来自 json 还是我的脚本,以及在这两种情况下要更改什么?
FYI the json is coming from Alfresco auditing api.
仅供参考,json 来自 Alfresco 审计 api。
采纳答案by Bill Ruppert
It works if the Audit... and admin values are quoted. The line
如果引用了 Audit... 和 admin 值,它会起作用。线
my @json = @{decode_json($result)};
needs to be just
需要公正
my @json = decode_json($result);
回答by Quentin
This:
这个:
"application":AuditExampleLogin1,
… is invalid JSON. AuditExampleLogin1isn't an array, object, number, string or atom. I don't know what it is supposed to be, so I can't tell you what to change it to.
... 是无效的 JSON。AuditExampleLogin1不是数组、对象、数字、字符串或原子。我不知道它应该是什么,所以我不能告诉你把它改成什么。
If it is a string, then you need to enclose it in ".
如果是字符串,则需要将其包含在".
See also: JSON Lint.
另请参阅:JSON Lint。

