以增量方式从页面读取 JSON
Reading JSON from pages incrementally
我有一个 java 客户端从 Web 服务读取 JSON。 json 响应的第一部分(偏移量 1 - 20)的格式如下。我目前可以将数据的第一页解析成一个文件(尽管我最终会喜欢解析成数据库)。如何让客户端解析下一页(偏移量 21 - 40)?为简洁起见,文件的第二部分有意省略了更多信息。我已经使用了 apache http 客户端,但似乎没有办法处理这个问题?指点不胜感激
{
"meta": {
"limit": 15,
"next": "/abc/api/defapi/lkk/?username=abc&api_key=xyz&limit=15&offset=60&format=json",
"offset": 30,
"previous": "/abc/api/defapi/lkk/?username=abc&api_key=xyz&limit=15&offset=30&format=json",
"total_count": 312213
},
"objects": [
{
"availability": "COMPLETE",
"confidentiality": "",
"cpe_id": [
{
"name": "cpe:/o:sun:sunos:5.11::express"
}
NOTE:truncated for brevity
非常感谢所有对我的问题的回答和指点。下面的代码完成了我想要的,即抓取 json 文件并逐步保存在磁盘上。我认为它仍然是非常基础的,因此我会感谢所有对更好的方法和编码风格的批评。还在学习中!!
while (count < totalCount ) {
MyClient check = new MyClient();
check.checkSecurity();
System.out.println(" ************ Starting with count " + count + " ***********************");
URI uriBuilder = new URIBuilder(url)
.addParameter("limit", String.valueOf(limit))
.addParameter("offset", String.valueOf(offset))
.build();
System.out.println("The URL built is --> " + uriBuilder);
HttpGet hget = new HttpGet(uriBuilder);
System.out.println(hget.getRequestLine());
CloseableHttpClient httpclient = HttpClients.createDefault();
hget.addHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(hget);
URL vdbUrl = uriBuilder.toURL();
System.out.println("Response status : " + response.getStatusLine());
HttpEntity entity2 = response.getEntity();
do {
vdbInput = new File("C:/myfiles/entry_0" + filenamePostfix + ".json");
FileUtils.copyURLToFile(vdbUrl, vdbInput);
System.out.println("File successfully written from page " + count );
System.out.println("page is : " + vdbUrl);
filenamePostfix++;
count++;
offset = offset + 20;
} while (filenamePostfix < count) ;
EntityUtils.consume(entity2);
}
}
我有一个 java 客户端从 Web 服务读取 JSON。 json 响应的第一部分(偏移量 1 - 20)的格式如下。我目前可以将数据的第一页解析成一个文件(尽管我最终会喜欢解析成数据库)。如何让客户端解析下一页(偏移量 21 - 40)?为简洁起见,文件的第二部分有意省略了更多信息。我已经使用了 apache http 客户端,但似乎没有办法处理这个问题?指点不胜感激
{
"meta": {
"limit": 15,
"next": "/abc/api/defapi/lkk/?username=abc&api_key=xyz&limit=15&offset=60&format=json",
"offset": 30,
"previous": "/abc/api/defapi/lkk/?username=abc&api_key=xyz&limit=15&offset=30&format=json",
"total_count": 312213
},
"objects": [
{
"availability": "COMPLETE",
"confidentiality": "",
"cpe_id": [
{
"name": "cpe:/o:sun:sunos:5.11::express"
}
NOTE:truncated for brevity
非常感谢所有对我的问题的回答和指点。下面的代码完成了我想要的,即抓取 json 文件并逐步保存在磁盘上。我认为它仍然是非常基础的,因此我会感谢所有对更好的方法和编码风格的批评。还在学习中!!
while (count < totalCount ) {
MyClient check = new MyClient();
check.checkSecurity();
System.out.println(" ************ Starting with count " + count + " ***********************");
URI uriBuilder = new URIBuilder(url)
.addParameter("limit", String.valueOf(limit))
.addParameter("offset", String.valueOf(offset))
.build();
System.out.println("The URL built is --> " + uriBuilder);
HttpGet hget = new HttpGet(uriBuilder);
System.out.println(hget.getRequestLine());
CloseableHttpClient httpclient = HttpClients.createDefault();
hget.addHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(hget);
URL vdbUrl = uriBuilder.toURL();
System.out.println("Response status : " + response.getStatusLine());
HttpEntity entity2 = response.getEntity();
do {
vdbInput = new File("C:/myfiles/entry_0" + filenamePostfix + ".json");
FileUtils.copyURLToFile(vdbUrl, vdbInput);
System.out.println("File successfully written from page " + count );
System.out.println("page is : " + vdbUrl);
filenamePostfix++;
count++;
offset = offset + 20;
} while (filenamePostfix < count) ;
EntityUtils.consume(entity2);
}
}