开发者

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

When I do:

开发者_如何学Pythonuse strict;
use JSON;
$json_ref = $json->decode($json_data);

My $json_ref structure is created with strings as hash refs. I view this via Data::Dumper, ie:

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

Is there a way to decode the JSON so it's not using strings as hash refs? Or do I just not enable stricts while working with JSON data in perl?


Seems to work OK for me, can you please post your example JSON and what you get?

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'
        }
        };


I've had this problem when doing a POST of JSON to Catalyst::Controller::REST when I didn't set the correct content-type of 'application/json'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜