简体   繁体   中英

how to read data from a json file which is specified with a particular URL using java

a json file is located online, and i want to read the data from that json file using java.

example json file which is available online : jscon file link

thank you

URL url = new URL("http://rate-exchange.appspot.com/currency?from=USD&to=EUR&q=3");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
    System.out.println(inputLine);
}
in.close();
Gson gson = new Gson();
Rate r = gson.fromJson(inputLine,Rate.class);

First obtain the string directly from the URL. I believe you could use this:

link here

Then from the string use the json library to build the json object. You might look into gson for more complex serialization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM