开发者

How To Save JSON text from Bing Using perl

Hi i am new with perl i have a problem to save text from BING json

here

 use strict;
 use warnings;
 use LWP::UserAgent;
 my $uagent = LWP::UserAgent->new;
 my $bing = $uagent->get("http://api.bing.com/osjson.aspx?query=microsoft")->content;
 print "$bing\n";

i want to save to text file line by line only a

  • microsoft
  • microsoft security essentials
  • microsoft update
  • microsoft office
  • microsoft downloads
  • micr开发者_JAVA技巧osoft word
  • microsoft templates
  • microsoft updates


The JSON module can be used to parse the data into a Perl structure and then print out the required array elements. Add this code to the bottom of your script.

use JSON;
my $data = decode_json($bing);
print join ',', @{ $data->[1] };


First you want to install the JSON module from CPAN. You can do this by typing the following on the command line (assuming linux)

$ cpan JSON

Then you want to decode the JSON string into an object using the decode function

my $json = JSON->decode($bing);

I'm not really sure what you wanted to do from here but to save data to a text file you must first open a file handle

open my $FH, '>', 'filename.txt' or die "Failed to open file: $!";

Then to write a message to it you simply print to the filehandle you just opened ($FH)

print $FH 'This will be in filename.txt';

*Note: * There is no comma between the $FH and the value being written to the file

Hope that helps

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜