java - 从远程网站读取 JSON 文件

标签 java json

我正在制作一个 java 程序,我正在寻找一种方法来检查是否有可用的新版本我在 parse.com 上创建了一个帐户。
API:CLICK
我完全在 java 上工作,我有点新,所以保持简单。
我已经创建了一个带有版本号的类,如果现在的版本是 beta,我需要一种方法将这 2 个值转换为 int 和 boolean 并进行其余的处理。 我如何从 parse.com 类中获取值?

最佳答案

以最简单的方式,您可以使用 json.org 提供的库对于 java(下载 here)并使用类似于以下的代码:

URL url = new URL("http://my.domain.com/data.json");
JSONTokener tokener = new JSONTokener(url.openStream());
JSONObject root = new JSONObject(tokener);

当然,您可以使用其他一些提供更多灵 active 的库,例如 GsonJackson

可以找到 gson 和 jackson 的小样本 herehere分别。

对于基本身份验证,您可以使用类似的东西:

Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { 返回新的 PasswordAuthentication ("usr", "pass".toCharArray()); } });

相关的东西heredocumentation .

此外,HTTPClient 是一个很棒的 HTTP 相关库,请检查它 here如果你愿意的话。


如果您使用的是 Maven,则可以为 json.org 添加以下依赖项:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

Gson :

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.2</version>
</dependency>

Jackson(主要版本 1,但 2 已经可用,请在网站上查看):

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.12</version>
</dependency>

对于 HTTP 客户端版本 4:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.4</version>
</dependency>

关于java - 从远程网站读取 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16244171/

相关文章:

javascript - 如何在 React-Native 中从 firebase 返回一组 child

java - Spring Data JPA 如何使用 Kotlin nulls 而不是 Optional

java - Hadoop( yarn ): Set mapper input separator?

c# - 我怎样才能将其重构为更易于管理的代码?

JSON 序列化值转换不使用 EF Core 跟踪更改

JSON.simple API Javadoc

java - 是否可以在数组中创建动态列数?

java - 修改正则表达式以获得更好的匹配

在 ubuntu 中下载 gradle 插件时出现 Java 安全问题

c# - 如何反转 JSON JTOKEN C#