Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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 获取数组的最后一个元素_Java_Database_Postman - Fatal编程技术网

Java 获取数组的最后一个元素

Java 获取数组的最后一个元素,java,database,postman,Java,Database,Postman,通过API,我得到了一个包含大量不同数据的数组。但我只需要最后一个元素 变量sb中包含数组。但是我不能访问这样的元素:sb[0](例如) 如果我打印变量sb,它如下所示: {"data":[[[1583596801195,279.52],[1583596814340,279.52],[1583596815535,279.44563849372383],[1583596816730,279.2060000000001],[1583596913525,279.2060000000001],[1583

通过API,我得到了一个包含大量不同数据的数组。但我只需要最后一个元素

变量sb中包含数组。但是我不能访问这样的元素:sb[0](例如)

如果我打印变量sb,它如下所示:

{"data":[[[1583596801195,279.52],[1583596814340,279.52],[1583596815535,279.44563849372383],[1583596816730,279.2060000000001],[1583596913525,279.2060000000001],[1583596914720,279.28824435146447],[1583596915915,279.52],[1583597211080,279.52],[1583597212275,279.52000000000004],[1583597213470,279.52],[1583597609015,279.52],[1583597610210,279.5199999999999],[1583597707005,279.5199999999999],[1583597708200,279.52000000000004],[1583597709395,279.52],[1583597806190,279.52],[1583597807385,279.52000000000004],[1583597993805,279.52000000000004]]]}
    URL url = new URL("the URL i get the data from");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    InputStream instream = con.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            instream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println(sb);
在本例中,我只需要最后一个元素(279.5200000000004)

我的代码如下所示:

{"data":[[[1583596801195,279.52],[1583596814340,279.52],[1583596815535,279.44563849372383],[1583596816730,279.2060000000001],[1583596913525,279.2060000000001],[1583596914720,279.28824435146447],[1583596915915,279.52],[1583597211080,279.52],[1583597212275,279.52000000000004],[1583597213470,279.52],[1583597609015,279.52],[1583597610210,279.5199999999999],[1583597707005,279.5199999999999],[1583597708200,279.52000000000004],[1583597709395,279.52],[1583597806190,279.52],[1583597807385,279.52000000000004],[1583597993805,279.52000000000004]]]}
    URL url = new URL("the URL i get the data from");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    InputStream instream = con.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            instream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println(sb);
对不起,我没有编程经验。但如果有人能帮助我,我会非常感激


谢谢您的帮助。

如果您只需要最后一个元素,则不应附加结果

而是替换以前存储的值

String result = null;
String line = null;
try {
    while ((line = reader.readLine()) != null) {
        //sb.append(line + "\n");
        result = line; // not appending, but replacing
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        instream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
System.out.println(result);

您可以尝试这样做,将此行“
System.out.println(sb)
替换为以下代码:

String s = new String(sb);
String d[] = s.split(",");  
System.out.println(d[d.length -1].replaceAll("]", ""));

这将打印您想要的确切数据,即
279.5200000000004

是的,这是有意义的。但问题是我没有访问存储这些元素的数据库的权限。@virw这有什么关系,因为您需要最后的结果,而您将仅在最后一次迭代时存储该结果。对不起,我不知道我是否理解它如果我这样做,仍然会得到相同的结果。这看起来像json数据,您是否尝试将其解析为json?