-1

I would like to pass some values i have from a string in to double variables. the string output looks like this:

{
    "high":"1635.07",
    "last":"1635.07",
    "timestamp":"1489299397",
    "volume":"321.34139374",
    "vwap":"1602.72987907",
    "low":"1595.03",
    "ask":"1635.89",
    "bid":"1605.10"
}

I just want this data to be like:

double high = (value of high in string);
double last = (value of last in string);

ect...

Im having trouble as java throws an error I believe because of the mix of words and numbers. Thanks in advance for the help.

code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.JOptionPane;


public class btc {

        private final String USER_AGENT = "Mozilla/5.0";

        public static void main(String[] args) throws Exception {

            btc http = new btc();
            http.sendGet();

        }

        // HTTP GET request
        private void sendGet() throws Exception {

            String url = "https://api.quadrigacx.com/v2/ticker?book=btc_cad";

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // optional default is GET
            con.setRequestMethod("GET");

            //add request header
            con.setRequestProperty("User-Agent", USER_AGENT);

            System.out.println("\nSending 'GET' request to URL : " + url);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }

            //write to variables


            String test = response.toString();

            //double high = test("high");

            //Double high = Double.parseDouble(test);

            System.out.println(test);
            //print result
            //JOptionPane.showMessageDialog(null, response.toString());

        }


}
MeIr
  • 7,236
  • 6
  • 47
  • 80
bumsey
  • 3
  • 2

1 Answers1

0

As already mentioned in the comments what you receiving from the server is a JSON object as documented in QuadrigaCX's API description so it should be parsed as such as the order of the members may vary aswell as the whitespace.

What's interesting about this JSON string is that all values are actually strings as they are enclosed in double quotation marks. But these strings contain values that can be interpreted and parsed as double.

Using minimal-json, which is a minimalistic Java library that allows you to parse JSON and access contained values directly. The following code makes use of it and "reads" high and last as double values:

JsonObject jsonObject = Json.parse(responseBody).asObject();
double high = Double.parseDouble(jsonObject.get("high").asString());
double last = Double.parseDouble(jsonObject.get("last").asString());

Here responseBody corresponds to what you have named test in your sendGet method and is the response from the web server as one string.

Markus Benko
  • 1,507
  • 8
  • 8