You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android天气APP:如何用Java提取API响应中同名JSON对象到数组

解决OpenWeatherAPI JSON嵌套对象提取问题

方法一:用Android自带org.json手动提取

直接遍历API返回的list数组,逐个取出目标嵌套对象存入集合,操作直观且无需额外依赖:

// 假设responseStr是API返回的完整JSON字符串
JSONObject responseObj = new JSONObject(responseStr);
JSONArray listArray = responseObj.getJSONArray("list");

JSONArray weatherArray = new JSONArray();

for (int i = 0; i < listArray.length(); i++) {
    JSONObject listItem = listArray.getJSONObject(i);
    // OpenWeatherAPI的weather字段是数组,通常取第一个元素
    JSONArray weatherItemArray = listItem.getJSONArray("weather");
    if (weatherItemArray.length() > 0) {
        JSONObject weatherObj = weatherItemArray.getJSONObject(0);
        weatherArray.put(weatherObj);
    }
}

// 按索引访问指定对象的信息
String targetDescription = weatherArray.getJSONObject(1).getString("description"); // 对应第2条数据

方法二:使用Gson实现提取

如果之前用Gson没找到思路,试试这两种方式:

方式A:通过实体类间接提取

先定义匹配JSON结构的实体类(仅保留需要的字段即可):

public class WeatherResponse {
    private List<ListItem> list;

    public static class ListItem {
        private List<Weather> weather;
        // 其他字段如main可按需添加

        public static class Weather {
            private String description;
            // 其他字段如id、main可按需添加

            public String getDescription() { return description; }
        }
    }
}

解析后遍历提取目标对象:

Gson gson = new Gson();
WeatherResponse response = gson.fromJson(responseStr, WeatherResponse.class);

List<WeatherResponse.ListItem.Weather> weatherList = new ArrayList<>();
for (WeatherResponse.ListItem item : response.getList()) {
    if (item.getWeather() != null && !item.getWeather().isEmpty()) {
        weatherList.add(item.getWeather().get(0));
    }
}

// 按索引访问
String targetDesc = weatherList.get(4).getDescription();

方式B:直接操作JsonElement(无需实体类)

如果不想定义实体类,直接用Gson的JSON节点API遍历:

Gson gson = new Gson();
JsonElement rootElement = gson.fromJson(responseStr, JsonElement.class);
JsonArray listArray = rootElement.getAsJsonObject().getAsJsonArray("list");

List<JsonObject> weatherList = new ArrayList<>();
for (JsonElement element : listArray) {
    JsonObject listItem = element.getAsJsonObject();
    JsonArray weatherArray = listItem.getAsJsonArray("weather");
    if (weatherArray != null && weatherArray.size() > 0) {
        weatherList.add(weatherArray.get(0).getAsJsonObject());
    }
}

// 访问指定索引的字段
String targetDesc = weatherList.get(27).get("description").getAsString();

方法三:使用Jackson实现提取

Jackson的思路和Gson类似,两种实现方式:

方式A:实体类方式

复用上面定义的WeatherResponse实体类,解析后提取:

ObjectMapper mapper = new ObjectMapper();
WeatherResponse response = mapper.readValue(responseStr, WeatherResponse.class);

List<WeatherResponse.ListItem.Weather> weatherList = new ArrayList<>();
for (WeatherResponse.ListItem item : response.getList()) {
    if (item.getWeather() != null && !item.getWeather().isEmpty()) {
        weatherList.add(item.getWeather().get(0));
    }
}

String targetDesc = weatherList.get(1).getDescription();

方式B:直接操作JsonNode

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(responseStr);
JsonNode listNode = rootNode.get("list");

List<JsonNode> weatherList = new ArrayList<>();
for (JsonNode itemNode : listNode) {
    JsonNode weatherArrayNode = itemNode.get("weather");
    if (weatherArrayNode != null && weatherArrayNode.isArray() && weatherArrayNode.size() > 0) {
        weatherList.add(weatherArrayNode.get(0));
    }
}

// 按索引获取描述
String targetDesc = weatherList.get(4).get("description").asText();

关键注意点

  • OpenWeatherAPI的weather字段本身是数组(支持多天气状况,但多数场景下只有一个元素),所以必须先取数组的第一个元素
  • 如果需要提取main等其他嵌套对象,只需将代码中的weather替换为对应字段名即可

内容的提问来源于stack exchange,提问作者kamilio3214

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 17:30:21