由于编码问题,decode_json错误获取错误

时间:2016-04-27 13:04:15

标签: json perl

执行以下代码时出错:

use JSON;
use Encode qw( encode decode encode_utf8 decode_utf8);
my $arr_features_json = '[{"family":"1","id":107000,"unit":"","parent_id":"0","cast":"2","search_values_range":"1,2,3,4,5,6,7,8,9,10,11,12","category_id":"29","type":"2","position":"3","name":"Número de habitaciones","code":"numberofrooms","locales":"4","flags":"1"}]';
$arr_features_json = decode_json( $arr_features_json );

以下是我得到的错误:

  

JSON字符串中的UTF-8字符格式不正确,在test.pl第13行的字符偏移量169处(在“\ x {fffd} de habitaci ...”之前)。

decode_json由于json中的ú字符而发出错误,因此我想将此字符转换为\u00fa。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

decode_json需要UTF-8,但您使用的字符串不使用UTF-8进行编码。如果字符串尚未decode,则使用from_json代替decode_json

#!/usr/bin/perl

use strict;
use warnings;
use feature qw( say );

use utf8;                             # Perl code is encoded using UTF-8.
use open ':std', ':encoding(UTF-8)';  # Terminal provides/expects UTF-8.

use JSON qw( from_json );

my $features_json = '
  [
    {
      "family": "1",
      "id": 107000,
      "unit": "",
      "parent_id": "0",
      "cast": "2",
      "search_values_range": "1,2,3,4,5,6,7,8,9,10,11,12",
      "category_id": "29",
      "type": "2",
      "position": "3",
      "name": "Número de habitaciones",
      "code": "numberofrooms",
      "locales": "4",
      "flags": "1"
    }
  ]
';

my $features = from_json( $features_json );

say $features->[0]{name};

答案 1 :(得分:1)

错误表示您尝试处理的字符串不是UTF-8或错误的UTF-8字符串。因此,在将其解码为json之前,需要使用encode_utf8将其转换为UTF-8字符串。

use JSON;
use Data::Dumper;
use Encode qw( encode decode encode_utf8 decode_utf8);

my $arr_features_json = '[{"family":"1","id":107000,"unit":"","parent_id":"0","cast":"2","search_values_range":"1,2,3,4,5,6,7,8,9,10,11,12","category_id":"29","type":"2","position":"3","name":"Número de habitaciones","code":"numberofrooms","locales":"4","flags":"1"}]';
my $arr_features = decode_json( encode_utf8($arr_features_json) );

print Dumper($arr_features);

您可能应该检查此article以了解UTF-8字符串与character strings之间的区别。