【问题标题】:How to get the last entry of a field in JSON如何在 JSON 中获取字段的最后一个条目
【发布时间】:2021-07-29 15:02:12
【问题描述】:

我使用来自 api.nasa.gov 的 API,我使用以下 Java 类查询该 API 以读取当天最新太阳耀斑的幅度信息。

该响应用于填写气象站上的一个字段,用于告知当前的太阳危害。

问题是这段代码只返回当天第一个“flrID”(太阳耀斑)事件的“classType”(幅度)。例如,7 月 3 日有不止一个事件,而最相关的事件是最新的。

鉴于 flrID 文本包含一天中未知的任意时间,我正在尝试找出如何获取 JSON 字符串中最后一个“flrID”的“classType”。

其次,我不知道如何从 NASA 获取事件驱动的更新,而不是从推送的电子邮件消息中获取。我被允许每 3.6 秒发出一次 API 请求,这样就可以了,但我正在询问是否有更便宜的方法可以从 NASA 获得近乎实时的更新。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.json.JSONException;
import org.json.JSONObject;

public class NasaFlrApiReader {

    public static final String API_KEY = "DEMO_KEY";

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        try (InputStream is = new URL(url).openStream()) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            String jsonText = readAll(rd);
            int i = jsonText.indexOf("{");
            jsonText = jsonText.substring(i);
            JSONObject json = new JSONObject(jsonText);            
            return json;
        }
    }

    public static void main(String[] args) throws IOException, JSONException {
        JSONObject json = readJsonFromUrl("https://api.nasa.gov/DONKI/FLR?startDate=2021-07-27-03&endDate=2021-07-03&api_key=" + API_KEY);
        System.out.println("Class Type: " + json.get("classType"));
    }
}

这是从上述代码返回的 JSON 文本:

[
  {
    "flrID": "2021-07-03T02:14:00-FLR-001",
    "instruments": [
      {
        "displayName": "GOES-P: EXIS 1.0-8.0"
      }
    ],
    "beginTime": "2021-07-03T02:14Z",
    "peakTime": "2021-07-03T02:31Z",
    "endTime": "2021-07-03T02:39Z",
    "classType": "C5.7",
    "sourceLocation": "N23W75",
    "activeRegionNum": 12838,
    "linkedEvents": [
      {
        "activityID": "2021-07-03T03:48:00-CME-001"
      }
    ],
    "link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17197/-1"
  },
  {
    "flrID": "2021-07-03T07:04:00-FLR-001",
    "instruments": [
      {
        "displayName": "GOES-P: EXIS 1.0-8.0"
      }
    ],
    "beginTime": "2021-07-03T07:04Z",
    "peakTime": "2021-07-03T07:17Z",
    "endTime": "2021-07-03T07:22Z",
    "classType": "M2.7",
    "sourceLocation": "N23W78",
    "activeRegionNum": 12838,
    "linkedEvents": [
      {
        "activityID": "2021-07-03T08:00:00-CME-001"
      }
    ],
    "link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17189/-1"
  },
  {
    "flrID": "2021-07-03T14:18:00-FLR-001",
    "instruments": [
      {
        "displayName": "GOES-P: EXIS 1.0-8.0"
      }
    ],
    "beginTime": "2021-07-03T14:18Z",
    "peakTime": "2021-07-03T14:29Z",
    "endTime": "2021-07-03T14:34Z",
    "classType": "X1.5",
    "sourceLocation": "N23W80",
    "activeRegionNum": 12838,
    "linkedEvents": [
      {
        "activityID": "2021-07-03T14:48:00-CME-001"
      }
    ],
    "link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17201/-1"
  },
  {
    "flrID": "2021-07-03T16:59:00-FLR-001",
    "instruments": [
      {
        "displayName": "GOES-P: EXIS 1.0-8.0"
      }
    ],
    "beginTime": "2021-07-03T16:59Z",
    "peakTime": "2021-07-03T17:03Z",
    "endTime": "2021-07-03T17:14Z",
    "classType": "M1.0",
    "sourceLocation": "N23W82",
    "activeRegionNum": 12838,
    "linkedEvents": [
      {
        "activityID": "2021-07-03T17:36:00-CME-001"
      }
    ],
    "link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17208/-1"
  }
]

【问题讨论】:

  • int i = jsonText.indexOf("{"); - 为什么?读入所有文本(这是一个数组),然后尝试获取值
  • 尝试将您的 json 文本发布到 jsoneditoronline.org - 然后您就可以看到如何访问它

标签: java json


【解决方案1】:

JSON 包含一个数组,因此在反序列化过程中,您需要使用JSONArray,然后使用length-1 获取数组的最后一个元素,

 JSONArray jsonArr = new JSONArray(jsonText);
 JSONObject lastObject = (JSONObject) jsonArr.get(jsonArr.length()-1);

如果length-1 没有给你最后一条记录,那么你需要根据必填字段对 JSONArray 进行排序,然后根据排序取第一个或最后一个。您可以检查以下线程以对JSONArray 进行排序 How can I sort a JSONArray in JAVA

【讨论】:

    猜你喜欢
    • 2022-10-19
    • 1970-01-01
    • 2014-06-29
    • 2013-10-24
    • 2021-12-09
    • 2020-03-31
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多