最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Where can I find a Perl module for converting a Perl data structure into a JavaScript one? - Stack Overflow

programmeradmin2浏览0评论

Where can I find a Perl module for converting a Perl data structure into a JavaScript one?

e.g. this is my code (Mason):

% # convert our @cti data structure into a javascript one
  var cti = [
% foreach my $cti_category (@cti) {
             {
                 label: "<% $cti_category->{'label'} %>",
                 value: "<% $cti_category->{'value'} %>",
                 children: [
%     foreach my $cti_type (@{$cti_category->{'children'}}) {
                            {
                              label: "<% $cti_type->{'label'} %>",
                              value: "<% $cti_type->{'value'} %>",
                            },
%     }
                           ]
             },
% }
            ];

is there a module for this?

Where can I find a Perl module for converting a Perl data structure into a JavaScript one?

e.g. this is my code (Mason):

% # convert our @cti data structure into a javascript one
  var cti = [
% foreach my $cti_category (@cti) {
             {
                 label: "<% $cti_category->{'label'} %>",
                 value: "<% $cti_category->{'value'} %>",
                 children: [
%     foreach my $cti_type (@{$cti_category->{'children'}}) {
                            {
                              label: "<% $cti_type->{'label'} %>",
                              value: "<% $cti_type->{'value'} %>",
                            },
%     }
                           ]
             },
% }
            ];

is there a module for this?

Share Improve this question edited Nov 25, 2013 at 15:10 fthiella 49k15 gold badges95 silver badges107 bronze badges asked Sep 25, 2008 at 17:18 someguysomeguy 1,9435 gold badges21 silver badges19 bronze badges 1
  • Now that everybody's said JSON, how about adding the json tag to your question? :D – skiphoppy Commented Sep 25, 2008 at 18:46
Add a comment  | 

4 Answers 4

Reset to default 16

JSON stands for JavaScript Object Notation, which is the format you're looking for.

Unfortunately, none of the modules you're looking for are in the Perl core, but they are available on CPAN, as a quick search will reveal.

I'd actually recommend installing JSON::Any as a wrapper, as well as JSON::XS (if you have a C compiler) or one of JSON and JSON::Syck if you don't. JSON::Any provides an interface class on top of several other JSON modules (you can choose, or let it pick from what's installed) that's independent of which module you wind up using. That way, if your code should need to be ported elsewhere, and (say) the target machine can install JSON::XS when you can't, you get a performance boost without any extra code.

use JSON::Any;

my $j = JSON::Any->new;

$json = $j->objToJson($perl_data);

Like so.

Check out JSON or JSON::XS.

To elaborate a bit more, JSON is "JavaScript Object Notation", and the two modules above convert perl data structures into that format.

The JSON module will convert data structures - it's basically a to/from JSON serializer.

JSON !

This module converts Perl data structures to JSON and vice versa using either JSON::XS or JSON::PP.

发布评论

评论列表(0)

  1. 暂无评论