1

I am a beginner with Java and I try to make a currency conversion of popular currencies. To keep the rates updated I want to use this API:

http://openexchangerates.org/api/latest.json?app_id=db98850be67e4d3d9a3ac0cf26ea2e40

How can I use this API of the current currency rates?

Thomas Uhrig
  • 27,466
  • 11
  • 57
  • 76

2 Answers2

3

First of all, your question got nothing to do with Netbeans as it mentioned in its original version. Netbeans is just an IDE like Eclipse. Your question is only related to Java.

And to break your question down further, you do not need to put this "into" Netbeans or whatever. The API you mentioned is a web API which returns JSON. So the only thing you need to do is to request URLs (just like in the browser), parse the JSON string to an object and work with this object.

Here is an example how to request the URL/JSON and how to parse it to a JSON object:

public static void main(String[] args) throws Exception {
    String url = "http://openexchangerates.org/api/latest.json?app_id=db98850be67e4d3d9a3ac0cf26ea2e40";
    String json = new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next();
    JsonParser jsonParser = new JsonParser();
    System.out.println(jsonParser.parse(json));
}

The example uses Googles GSON librare which you find at https://code.google.com/p/google-gson.

Thomas Uhrig
  • 27,466
  • 11
  • 57
  • 76
  • How do I add Google GSON to my java program, else I get a whole of of errors, sorry I am very new at this and first time using APIs. – user3009505 Jun 04 '14 at 20:23
  • Download the ZIP file at https://google-gson.googlecode.com/files/google-gson-2.2.4-release.zip, unpack it, copy the file `gson-2.2.4.jar` to your project and put on the classpath (see http://stackoverflow.com/questions/5893349/java-how-to-add-library-files-in-netbeans). – Thomas Uhrig Jun 04 '14 at 20:48
  • Thanks, I got it working but I do not know how to go further with converting currencies, since i'm a beginner programmer. I guess it was worth a try. Would be awesome if you had teamviewer and could help me out. I am a beginner programmer and your help would be great if you wanted to. – user3009505 Jun 04 '14 at 21:24
2

I would recommend on Jackson:

http://jackson.codehaus.org/

its a great Java API for handling Json

Hovav
  • 131
  • 1
  • 10