繁体   English   中英

在不使用外部库的情况下获得Json中的特定元素

[英]Getting specific element in Json respone without using an external libary

我想创建一个GitHub更新检查器,以提供最新版本的版本。

public static void main(String[] args) {
    HttpURLConnection httpURLConnection = null;

    try {
        httpURLConnection = (HttpURLConnection) new URL("https://api.github.com/repos/MilkBowl/Vault/releases/latest").openConnection();

        httpURLConnection.setRequestMethod("GET");            
        httpURLConnection.setRequestProperty("Content-Type", "application/json");

        try (final InputStream inputStream = httpURLConnection.getInputStream(); final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); final BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {

        }
    } catch (final IOException exception) {
        exception.printStackTrace();
    } finally {
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
    }
}

这就是我到目前为止所得到的。 它返回以下Json响应:

https://api.github.com/repos/MilkBowl/Vault/releases/latest

现在,我想获取“ tag_name”。 如何使用普通Java做到这一点? 我不想使用外部库。

好吧,我的声誉不足以发表评论,所以...

我认为您可以使用正则表达式仅获取名称,可以使用如下形式:

name.+?,

然后应用一些替代品进行消毒。 像这样:

Pattern.compile("'name.+?,'").matcher(your data);
if (matcher.find()){
    System.out.println(matcher.group(1).replace("\"name\": \"", "").replace("\",", "");
}

暂无
暂无

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

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