繁体   English   中英

使用 Java 将 JSON 数据推送到文件

[英]Push JSON data to a file using Java

我正在使用 java 调用 REST API Get 方法,并且需要将 JSON 数据存储到文件中。 这是我的 JSON 数据:

{
 "took" : 25,
 "timed_out" : false,
 "_shards" : {
 "total" : 5,
 "successful" : 5,
 "failed" : 0
},
 "hits" : {
  "total" : 820,
  "max_score" : 1.0,
  "hits" : [ {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
    {"A":1,"B":12,"C":122,"D":89,"E":120,"F":100,"G":2}
  }, {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
    {"A":54,"B":171,"C":102,"D":0,"E":120,"F":11,"G":20}
 } , {
  "_index" : "test",
  "_type" : "status",
  "_id" : "abc-2345-dadasasdasd214124",
  "_score" : 1.0,
  "_source":
  {"A":94,"B":8,"C":155,"D":32,"E":90,"F":11,"G":0}
} ]
}
}

这是我的代码:

public class testcode {

public static void main(String[] args) throws ClientProtocolException, IOException {
      HttpClient client = new DefaultHttpClient();

      try {
          HttpGet httpGetRequest = new HttpGet("http://localhost:8080/test_search?pretty=1");
          HttpResponse httpResponse = client.execute(httpGetRequest);

          HttpEntity entity = httpResponse.getEntity();

          byte[] buffer = new byte[1024];
          if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
              int bytesRead = 0;
              BufferedInputStream bis = new BufferedInputStream(inputStream);
              FileWriter file = new FileWriter("/path/test.json");

              while ((bytesRead = bis.read(buffer)) != -1) {
                String chunk = new String(buffer, 0, bytesRead);
                System.out.println(chunk);
                file.write(chunk);


              }
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              try { inputStream.close(); } catch (Exception ignore) {}
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          client.getConnectionManager().shutdown();
        }

}

运行此代码后,我在控制台中看到了 JSON 数据,但未将整个数据保存到 test.json 文件中。 前两个数组被完全保存,但只有第三个数组的一部分被保存到 test.json 中。

test.json 文件中的数据:

{
 "took" : 25,
  "timed_out" : false,
  "_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
 "hits" : {
  "total" : 820,
  "max_score" : 1.0,
  "hits" : [ {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
     {"A":1,"B":12,"C":122,"D":89,"E":120,"F":100,"G":2}
   }, {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
     {"A":54,"B":171,"C":102,"D":0,"E":120,"F":11,"G":20}
   } , {
    "_index" : "test",
    "_type" : "status",
    "_id" : "abc-2345-dadasasdasd214124",
    "_score" : 1.0,
    "_source":
     {"A":94,"B":8,"C":155,"D":32,"E

注意:上面给出的 json 数据是一个示例数据。 真正的 json 文件包含大量数据。 请指教。 谢谢

还尝试确保FileWriter已关闭。

} finally {
  try { file.close(); inputStream.close(); } catch (Exception ignore) {}
}

您的程序可能正在退出,而某些数据仍处于缓冲状态且尚未写入文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM