Java 将从URL输出的JSON保存到文件

Java 将从URL输出的JSON保存到文件,java,python,ruby,perl,bash,Java,Python,Ruby,Perl,Bash,如何将URL输出的JSON保存到文件中 e、 g来自Twitter搜索API(此) 语言并不重要 编辑//然后如何将进一步的更新附加到EOF 编辑2//Great answers伙计们真的很好,但我接受了我认为最优雅的答案。这在任何语言中都很容易,但机制各不相同。使用wget和shell: wget 'http://search.twitter.com/search.json?q=hi' -O hi.json 附加: wget 'http://search.twitter.com/search

如何将URL输出的JSON保存到文件中

e、 g来自Twitter搜索API(此)

语言并不重要

编辑//然后如何将进一步的更新附加到EOF


编辑2//Great answers伙计们真的很好,但我接受了我认为最优雅的答案。

这在任何语言中都很容易,但机制各不相同。使用wget和shell:

wget 'http://search.twitter.com/search.json?q=hi' -O hi.json
附加:

wget 'http://search.twitter.com/search.json?q=hi' -O - >> hi.json
hi_web = urllib2.urlopen('http://search.twitter.com/search.json?q=hi');
with open('hi.json', 'ab') as hi_file:
  hi_file.write(hi_web.read())
使用Python:

urllib.urlretrieve('http://search.twitter.com/search.json?q=hi', 'hi.json')
附加:

wget 'http://search.twitter.com/search.json?q=hi' -O - >> hi.json
hi_web = urllib2.urlopen('http://search.twitter.com/search.json?q=hi');
with open('hi.json', 'ab') as hi_file:
  hi_file.write(hi_web.read())
在壳牌:

wget -O output.json 'http://search.twitter.com/search.json?q=hi'
下面是(verbose;))Java变体:

InputStream input = null;
OutputStream output = null;
try {
    input = new URL("http://search.twitter.com/search.json?q=hi").openStream();
    output = new FileOutputStream("/output.json");
    byte[] buffer = new byte[1024];
    for (int length = 0; (length = input.read(buffer)) > 0;) {
        output.write(buffer, 0, length);
    }
    // Here you could append further stuff to `output` if necessary.
} finally {
    if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
    if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
另见

您可以使用

在PHP中:

$outfile= 'result.json';
$url='http://search.twitter.com/search.json?q=hi';
$json = file_get_contents($url);
if($json) { 
    if(file_put_contents($outfile, $json, FILE_APPEND)) {
      echo "Saved JSON fetched from “{$url}” as “{$outfile}”.";
    }
    else {
      echo "Unable to save JSON to “{$outfile}”.";
    }
}
else {
   echo "Unable to fetch JSON from “{$url}”.";
}
您可以使用:

ObjectMapper mapper=new ObjectMapper();
Map Map=mapper.readValue(url,Map.class);
writeValue(新文件(“myfile.json”)、map;

下面是使用PHP和fOpen实现这一点的另一种方法

<?php
// Define your output file name and your search query
$output = 'result.txt';
$search = 'great';

write_twitter_to_file($output, $search);

/*
 * Writes Json responses from twitter API to a file output.
 * 
 * @param $output: The name of the file that contains the output 
 * @param $search: The search term query to use in the Twitter API
*/

function write_twitter_to_file($output, $search) {
    $search = urlencode($search);
    $url = 'http://search.twitter.com/search.json?q=' . $search;
    $handle = fopen($url, "r");

    if ($handle) {
        while (($buffer = fgets($handle, 4096)) !== false) {
            file_put_contents($output, $buffer, FILE_APPEND);
            echo "Output has been saved to file<br/>";
        }

        if (!feof($handle)) {
            echo "Error: unexpected fgets() fail\n";
        }

        fclose($handle);
    }

}
?>

只需输出到stdout(
-
)并使用追加重定向操作符(
>
)。