开发者

Using the Perl module JSON to create a JSON object

I'm having a killer time trying to create a JSON object to return to a jQuery request.

I'm trying to use the jQuery chain开发者_Go百科ed select module

and I'm trying to create the JSDN object using Perl's JSON module.

I have no idea what I'm doing wrong or how I can even debug it, about the best I can do is get a JS dialog box coming up with "A unknown error....".

This is all I was trying to do to create the JSON object, I thought this would be all that is needed, but it's not. Any ideas?

TIA

use JSON;
my %data = (1 => 'val1',
            2 => 'val2',
            3 => 'val3',
           );
my $json_text = to_json(\%data);
print $json_text;


Running the script gives:

{"1":"val1","3":"val3","2":"val2"}

… which is the expected output.

The problem almost certainly lies with whatever code you are using to get the JSON from your server to the client.


Have the javascript output the response text before processing to a textarea so you can see if there's something failing in transport. Also, you're going to want to put something in front of the data, like "myVar= " so that the data is assigned to something on the other end.


Looks like you already figured it out your issue, and if you were wondering why it wasn't working is because the to_json method is not exported from JSON.pm in the older versions of the JSON module. The latest version on CPAN (2.0) exports to_json into your namespace but versions ~1.5 don't and require the JSON::. Many repos still carry this older version, and if you are running on CentOS ~5.3 or an older version of Fedora Core you will have the older JSON.pm.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜