Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从页面增量读取JSON_Java_Json_Api_Rest_Httprequest - Fatal编程技术网

Java 从页面增量读取JSON

Java 从页面增量读取JSON,java,json,api,rest,httprequest,Java,Json,Api,Rest,Httprequest,我有一个从web服务读取JSON的java客户机。JSON响应的第一部分(偏移量1-20)的格式如下。目前,我可以将数据的第一页解析成一个文件(尽管我最终会喜欢解析成db)。如何让客户端解析下一页(偏移量21-40)?为了简洁起见,文件的第二部分故意省略了更多信息。我使用了ApacheHTTP客户端,但似乎没有方法来处理这个问题?如果指向正确的方向,我将不胜感激 { "meta": { "limit": 15, "next": "/abc/api/defa

我有一个从web服务读取JSON的java客户机。JSON响应的第一部分(偏移量1-20)的格式如下。目前,我可以将数据的第一页解析成一个文件(尽管我最终会喜欢解析成db)。如何让客户端解析下一页(偏移量21-40)?为了简洁起见,文件的第二部分故意省略了更多信息。我使用了ApacheHTTP客户端,但似乎没有方法来处理这个问题?如果指向正确的方向,我将不胜感激

{
    "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);
                }
        }
while(计数”+uriBuilder);
HttpGet hget=新的HttpGet(uriBuilder);
System.out.println(hget.getRequestLine());
CloseableHttpClient httpclient=HttpClients.createDefault();
hget.addHeader(“内容类型”、“应用程序/json”);
HttpResponse response=httpclient.execute(hget);
URL vdbUrl=uriBuilder.toURL();
System.out.println(“响应状态:+Response.getStatusLine());
HttpEntity entity2=response.getEntity();
做{
vdbInput=新文件(“C:/myfiles/entry_0”+filenamePostfix+”.json”);
copyrltofile(vdbUrl、vdbInput);
System.out.println(“从页面成功写入文件”+计数);
System.out.println(“页面为:“+vdbUrl”);
filenamePostfix++;
计数++;
偏移量=偏移量+20;
}while(filenamePostfix
这不是json。前一个值缺少一个“,这意味着它在语法上不正确,因此无效。@MarcB…这是我的错误,对不起。我复制错了,我认为是一个有效的json响应。相应地更新。仍然不是有效的json,第一个
前面的
“meta”:
{
不应该在那里。@Henry..我已经相应地编辑了json,“meta”是json的一部分。非常感谢。抓取“下一个”url并进行另一个http调用。重复操作,直到没有“下一个”url为止。