【问题标题】:Parsing JSON response from an HTTP request with Gson in Java在 Java 中使用 Gson 解析来自 HTTP 请求的 JSON 响应
【发布时间】:2019-05-23 18:54:15
【问题描述】:

我正在尝试从 URL 读取以下 json 输出

{
    "error": false,
    "status": 200,
    "message": "License Key activated successfully.",
    "data": {
        "expire": 1582657054,
        "activation_id": 1519628117,
        "expire_date": "2020-02-25 18:57",
        "timezone": "UTC",
        "the_key": "Cqu62al903ICv40am9nM68Y7o9-32",
        "url": "http://domain/my-account/view-license-key/?key=test-32",
        "has_expired": false,
        "status": "active",
        "allow_offline": true,
        "offline_interval": "days",
        "offline_value": 1,
        "downloadable": {
            "name": "v1.1.5",
            "url": "https://domain/product-1.1.5.zip"
        },
        "ctoken": "dsfejk8989"
    }
}

我正在尝试获取“status: 200”和“activation_id”这两个值。

我尝试过在线查找和解析。似乎没有任何效果。我对整个 json 阅读有点陌生。

try {
    JSONParser jsonParser = new JSONParser();

    String jsonS = "";
    URL url = new URL(link);
    URLConnection conn = url.openConnection();
    conn.connect();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            conn.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        jsonS += inputLine;
    }

    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(jsonS, JsonObject.class);
    int id = jsonObject.get("status").getAsInt();

    cintout(id);
    cout(link);
    cout(inputLine);
    try {

        if (id == 200)
            return ValidationType.VALID;
        else
            return ValidationType.WRONG_RESPONSE;
    } catch (IllegalArgumentException exc) {

        if (id == 200)
            return ValidationType.VALID;
        else
            return ValidationType.WRONG_RESPONSE;
    }
} catch (IOException e) {
    e.printStackTrace();
    return ValidationType.VALID;
}

我已成功检索到状态值,但未检索到激活 ID。

【问题讨论】:

    标签: java json http parsing gson


    【解决方案1】:

    您使用了两个库进行 JSON 解析,这在此上下文中不需要。假设您想使用Gson。删除JSONParser jsonParser = new JSONParser();

    现在,您可以通过 Root -> data -> activation_id 访问您的 JSON 数据中的 activation_id。 Root 表示存储到jsonObject 的整个 JSON 对象。 data 键本身代表一个对象。因此,我们可以通过将 data 键值作为对象,然后将 activation_id 作为 int/string 来达到 activation_id

    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(jsonS, JsonObject.class);
    int id = jsonObject.get("status").getAsInt();
    int activationId = jsonObject.get("data").getAsJsonObject().get("activation_id").getAsInt();
    

    有关 json 对象的更多信息:https://www.shapediver.com/blog/json-objects-explained/

    【讨论】:

      【解决方案2】:

      您需要先使用 Gson 获取 data 对象,然后才能访问其字段:

      int activation_id = jsonObject.get("data").getAsJsonObject().get("activation_id").getAsInt();
      

      【讨论】:

        猜你喜欢
        • 2016-04-14
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 2015-10-10
        • 1970-01-01
        • 1970-01-01
        • 2015-06-15
        • 1970-01-01
        相关资源
        最近更新 更多