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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3040904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:55:07  来源:igfitidea点击:

Save JSON outputted from a URL to a file

javapythonrubyperlbash

提问by Skizit

How would I save JSON outputted by an URL to a file?

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

e.g from the Twitter search API (this http://search.twitter.com/search.json?q=hi)

例如来自 Twitter 搜索 API(这个http://search.twitter.com/search.json?q=hi

Language isn't important.

语言不重要。

edit // How would I then append further updates to EOF?

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

edit 2// Great answers guys really, but I accepted the one I thought was the most elegant.

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

采纳答案by Matthew Flaschen

This is easy in any language, but the mechanism varies. With wget and a shell:

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

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

To append:

附加:

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

With Python:

使用 Python:

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

To append:

附加:

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())

回答by Ignacio Vazquez-Abrams

In shell:

在外壳中:

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

回答by BalusC

Here's the (verbose ;) ) Java variant:

这是(详细;))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) {}
}

See also:

另见

回答by Bernie Perez

You can use CURL

您可以使用卷曲

curl -d "q=hi" http://search.twitter.com -o file1.txt

回答by user268396

In PHP:

在 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}”.";
}

回答by antonio

You can use Hymanson:

您可以使用Hyman逊

 ObjectMapper mapper = new ObjectMapper(); 
 Map<String,Object> map = mapper.readValue(url, Map.class);
 mapper.writeValue(new File("myfile.json"), map);

回答by Alex Centeno

Here is another way of doing this with PHP and fOpen.

这是使用 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);
    }

}
?>