簡體   English   中英

使用Perl JSON模塊解碼時,無法將字符串用作HASH ref錯誤

[英]Can't use string as a HASH ref error when using perl JSON module to decode

當我做:

use strict;
use JSON;
$json_ref = $json->decode($json_data);

我的$ json_ref結構是使用字符串作為哈希引用創建的。 我通過Data :: Dumper查看,即:

print STDERR "JSON: " . Dumper($json_ref);

有沒有一種方法可以解碼JSON,因此它不使用字符串作為哈希引用? 還是在perl中使用JSON數據時不啟用嚴格?

對於我來說似乎可以正常工作,請您發布示例JSON以及您能得到什么?

use strict;
# JSON example text from http://www.json.org/example.html
my $js = qq[

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
        "title": "S",
        "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                    },
                 "GlossSee": "markup"
                }
            }
        }
    }
}

];

use JSON;
use Data::Dumper;
my $json = new JSON();
my $json_ref = $json->decode($js);
print Data::Dumper->Dump([$json_ref]);

OUTPUT:

$VAR1 = {
  'glossary' => {
          'GlossDiv' => {
          'GlossList' => {
                   'GlossEntry' => {
                     'GlossDef' => {
                     'para' => 'A meta-markup language, used to create markup languages such as DocBook.',
                     'GlossSeeAlso' => [
                         'GML',
                         'XML'
                       ]
                   },
                     'GlossTerm' => 'Standard Generalized Markup Language',
                     'ID' => 'SGML',
                     'SortAs' => 'SGML',
                     'Acronym' => 'SGML',
                     'Abbrev' => 'ISO 8879:1986',
                     'GlossSee' => 'markup'
                   }
                 },
          'title' => 'S'
        },
          'title' => 'example glossary'
        }
        };

當我未將'application / json'的正確內容類型設置為JSON到Catalyst :: Controller :: REST時,我遇到了這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM