Java 从 URL 读取 JSON

标签 java json weather-api

我正在尝试使用我的天气 API 来获取某个区域的天气状况,我认为除了数据解析部分之外,我的所有功能都正常。

import java.net.*;
import java.io.*;
import com.google.gson.*;

public class URLReader {

    public static URL link;

    public static void main(String[] args) {
        try{
            open();
            read();
        }catch(IOException e){}
    }

    public static void open(){
        try{
            link = new URL("http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json");
        }catch(MalformedURLException e){}
    }
    public static void read() throws IOException{
        //little bit stuck here
    }
}

任何人都可以帮助我完成这个简单的小项目,顺便说一句,我是初学者。

最佳答案

您可以使用javaQuery更容易地做到这一点:

$.getJSON("http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json", null, new Function() {
    @Override
    public void invoke($ j, Object... args) {
        //if you are expecting a JSONObject, use:
        JSONObject json = (JSONObject) args[0];

        //otherwise, it would be: JSONArray json = (JSONArray) args[0];

        //Then to more easily parse the JSON, do this:
        Map<String, ?> map = $.map(json)

        //if you are using an array instead, you can use: Object[] array = $.makeArray(json);

        //Now just iterate through your map (or list) to get the data you want to parse.
    }
});

关于Java 从 URL 读取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19956348/

相关文章:

javascript - 如何使用开放天气 API 获取搜索城市的实时本地日期和时间

Java NIO 防止来自同一台 PC 的多个套接字连接

java - 高效的数据导入 PostgreSQL DB

python - 将 JSON 参数作为字符串传递给 python hadoop 流应用程序

python - 如何从 Scrapy 生成自定义 JSON 输出?

javascript - 一起显示英里每小时和公里每小时?

weather-api - OpenWeatherMap API与Wunderground API?

java - 找不到类 : org. junit.tests.AllTests - OSX

java - 如何刷新 SWT 标签?

java - 反对注解的论点