我遇到了一个奇怪的问题,如果我尝试转换一个JSON字符串(其布局如下: - )
{
"a" : "b",
"c" : ["" , ""]
}
JSON :: XS-> new->解码在这些情况下运行正常。但如果我有这样的东西要解码,
"
{
"women": ""
"men" : ""
}
"
我认为这些报价是这里的罪魁祸首。 它不断抛出应该有结束报价的错误。 这个JSON来自JS中的JSON.stringify,所以我不能让它工作。我已经看到JSON :: decode_json在这里工作了一些人,但我特别想让它全部使用JSON :: XS。最糟糕的情况是,我将从cpan导入JSON。
有什么建议吗?
编辑:我调查了这一点。这是我试图转换为perl HASH的JSON
{
"womens": [
"/sports/size-charts/a/B00EJ493TW/womens.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/womens.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/womens.html",
"/sports/size-charts/b/Yvette/womens.html",
"/sports/size-charts/c/SPORTING_GOODS/womens.html"
],
"mens": [
"/sports/size-charts/a/B00EJ493TW/mens.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/mens.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/mens.html",
"/sports/size-charts/b/Yvette/mens.html",
"/sports/size-charts/c/SPORTING_GOODS/mens.html"
],
"kids": [
"/sports/size-charts/a/B00EJ493TW/kids.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/kids.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/kids.html",
"/sports/size-charts/b/Yvette/kids.html",
"/sports/size-charts/c/SPORTING_GOODS/kids.html"
]
}
当我发送这个JSON时,它会获得HTML编码(引号被& 34替换;),这就是Perl因此错误而嘶嘶作响的地方: -
'"' expected, at character offset 1 [""womens":[&#..."]
非常感谢任何帮助
答案 0 :(得分:1)
您现在发布的JSON是有效的,可以通过JSON :: XS不会抛出错误(与您所说的相反)来确定。
use strict;
use warnings;
use Data::Dumper qw( Dumper );
use JSON::XS qw( decode_json );
my $json = <<'__EOI__';
{
"womens": [
"/sports/size-charts/a/B00EJ493TW/womens.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/womens.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/womens.html",
"/sports/size-charts/b/Yvette/womens.html",
"/sports/size-charts/c/SPORTING_GOODS/womens.html"
],
"mens": [
"/sports/size-charts/a/B00EJ493TW/mens.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/mens.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/mens.html",
"/sports/size-charts/b/Yvette/mens.html",
"/sports/size-charts/c/SPORTING_GOODS/mens.html"
],
"kids": [
"/sports/size-charts/a/B00EJ493TW/kids.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/kids.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/kids.html",
"/sports/size-charts/b/Yvette/kids.html",
"/sports/size-charts/c/SPORTING_GOODS/kids.html"
]
}
__EOI__
print(Dumper(decode_json($json)));
输出:
$VAR1 = {
'womens' => [
'/sports/size-charts/a/B00EJ493TW/womens.html',
'/sports/size-charts/b/Yvette/c/SPORTING_GOODS/womens.html',
'/sports/size-charts/c/SPORTING_GOODS/b/Yvette/womens.html',
'/sports/size-charts/b/Yvette/womens.html',
'/sports/size-charts/c/SPORTING_GOODS/womens.html'
],
'mens' => [
'/sports/size-charts/a/B00EJ493TW/mens.html',
'/sports/size-charts/b/Yvette/c/SPORTING_GOODS/mens.html',
'/sports/size-charts/c/SPORTING_GOODS/b/Yvette/mens.html',
'/sports/size-charts/b/Yvette/mens.html',
'/sports/size-charts/c/SPORTING_GOODS/mens.html'
],
'kids' => [
'/sports/size-charts/a/B00EJ493TW/kids.html',
'/sports/size-charts/b/Yvette/c/SPORTING_GOODS/kids.html',
'/sports/size-charts/c/SPORTING_GOODS/b/Yvette/kids.html',
'/sports/size-charts/b/Yvette/kids.html',
'/sports/size-charts/c/SPORTING_GOODS/kids.html'
]
};
您实际传递给JSON :: XS的JSON是不同的。它包含"women"
,您说它包含"women"
,适合初学者。
use strict;
use warnings;
use Data::Dumper qw( Dumper );
use JSON::XS qw( decode_json );
my $json = <<'__EOI__';
{
4womens4: [
"/sports/size-charts/a/B00EJ493TW/womens.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/womens.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/womens.html",
"/sports/size-charts/b/Yvette/womens.html",
"/sports/size-charts/c/SPORTING_GOODS/womens.html"
],
"mens": [
"/sports/size-charts/a/B00EJ493TW/mens.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/mens.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/mens.html",
"/sports/size-charts/b/Yvette/mens.html",
"/sports/size-charts/c/SPORTING_GOODS/mens.html"
],
"kids": [
"/sports/size-charts/a/B00EJ493TW/kids.html",
"/sports/size-charts/b/Yvette/c/SPORTING_GOODS/kids.html",
"/sports/size-charts/c/SPORTING_GOODS/b/Yvette/kids.html",
"/sports/size-charts/b/Yvette/kids.html",
"/sports/size-charts/c/SPORTING_GOODS/kids.html"
]
}
__EOI__
print(Dumper(decode_json($json)));
输出:
'"' expected, at character offset 4 (before "4womens4: ...") at a.pl line 33.
最糟糕的情况是,我将从cpan导入JSON。
JSON.pm只是JSON :: XS(如果已安装)或JSON :: PP的前端 (默认)。
切换到JSON.pm是没有用的,因为它最终会使用JSON :: XS。使用JSON :: PP或任何其他JSON解析器也无济于事;他们将无法处理JSON :: XS可以使用的损坏的JSON。
答案 1 :(得分:0)
我昨天提交了ikegami答案的编辑;但它没有显示出来,所以我猜它没有被批准。
我建议的是从ikegami的最终版本开始,并在顶部附近添加以下“使用”声明的其余部分:
use HTML::Entities qw( decode_entities ) ;
然后你所要做的就是将他的最后一行代码更新为:
print( Dumper( decode_json( decode_entities( $json ) ) ) ) ;
你会得到你正在寻找的结果。