“或}解析对象/哈希时预期"解析JSON文件时出错 [英] ", or } expected while parsing object/hash" error while parsing a JSON file

查看:224
本文介绍了“或}解析对象/哈希时预期"解析JSON文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印大型.json文件的名称"字段的内容,但解析时遇到问题.到目前为止,我已经尝试使用<:encoding(UTF-8)"打开.json文件,但到目前为止还没有运气.

I'm trying to print the contents of the "name" field of a large .json file and I'm having problems with the parsing. So far I've tried open the .json file using "<:encoding(UTF-8)", but no luck so far.

这是我的代码,错误在第11行:

This is my code, the error is on line 11:

#!/usr/bin/perl

use strict;
use warnings;
use JSON;

open(my $fh, "<:encoding(UTF-8)", "pokedex.json");
my $ejson = <$fh>;
close($fh);

my $djson = decode_json $ejson;
for(my $i=1;$i<=718;$i++){
    print $djson->{"$i"}{"name"}, "\n";
}

错误本身是, or } expected while parsing object/hash, at character offset 1 (before "\n") at jsonreverser.pl line 11.

我已经通过在线验证程序运行了.json文件,它说它的格式正确.这是指向json文件的链接: http://vps.zazez.com/junk/pokedex. json

I've ran the .json file through an online verifier, and it said it's correctly formatted. This is the link to the json file: http://vps.zazez.com/junk/pokedex.json

推荐答案

my $ejson = <$fh>;

是标量上下文中的赋值,因此您仅将输入文件的第一行加载到$ejson中.

is an assignment in scalar context, so you are only loading the first line of the input file into $ejson.

有很多方法可以将整个输入流加载到标量中:

There are many ways to load an entire input stream into a scalar:

my $ejson = join '', <$fh>;

my $ejson = do {
    local $/;
    <$fh>;
};

或按照asjo的建议使用File::Slurp.

Or use File::Slurp as asjo suggests.

这篇关于“或}解析对象/哈希时预期"解析JSON文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆