-1

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

  • [Reading from and Writing to a URLConnection](http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html) – gtgaxiola Mar 03 '15 at 15:35

2 Answers2

2
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);
Srinivasu
  • 1,215
  • 14
  • 32
Sireesh Vattikuti
  • 1,180
  • 1
  • 9
  • 21
1

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.

Community
  • 1
  • 1
Rafael Saraiva
  • 908
  • 2
  • 11
  • 23