1

I am trying to read the following input from this link: https://www.cryptopia.co.nz/api/GetMarket/ETH_BTC

{"Success":true,"Message":null,"Data":{"TradePairId":5203,"Label":"ETH/BTC","AskPrice":0.05711848,"BidPrice":0.05703892,"Low":0.05500000,"High":0.05733556,"Volume":1243.83039158,...

I need to extract the "TradePairId", and I could do this by splitting the fetched string with commas and putting that into a list, but this would be an ugly solution due to it being a 1D list with list.get(2) being

""Data":{"TradePairId":5203"

How could I analyze this entire 1 line list with another list inside of it cleanly and in the "right" way to access it's contents?

So far all I have is the following (the ugly way that isn't exactly reading the entire data as it should be):

    URL tickerHistory = new URL("https://www.cryptopia.co.nz/api/GetMarket/" + currencyPair.toString());
    URLConnection yc = tickerHistory.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine = in.readLine();
    List<String> tempList = Arrays.asList(inputLine.split(","));
StealthVice7
  • 95
  • 12
  • To be clear, the line I am analyzing has a list within a list ("Data" is the second list). I would like to be able to use every single individual thing in both lists, rather than hard coding to only get the TradePairId. – StealthVice7 Apr 08 '18 at 22:46

2 Answers2

0

You are going about this the entirely wrong way. The data within the curly brackets is in JSON format, and is meant to be interpreted as such! Splitting the string by commas is not the correct approach. I would highly suggest reading the following to get an understanding of what I'm talking about:

https://www.w3schools.com/js/js_json_intro.asp

Shane Sepac
  • 806
  • 10
  • 20
  • 2
    He is meaning to use Java.. so here is a potentially helpful article. http://www.oracle.com/technetwork/articles/java/json-1973242.html –  Apr 08 '18 at 22:48
  • Thanks a lot, I will definitely use the link you provided Ty Q, I will learn to fetch it the right way due to it being in JSON format. – StealthVice7 Apr 08 '18 at 23:00
  • Quick Question: This may be unrelated, but this looks like JSON too: https://www.cryptopia.co.nz/Exchange/GetTradePairChart?tradePairId=5203&dataRange=7&dataGroup=240 Would I fetch this in a similar way and make a list of lists for each square bracket data set? – StealthVice7 Apr 08 '18 at 23:04
  • @StealthVice7 You'll read about this shortly- but square brackets in JSON denote an array, and curly brackets indicate a JSON object. It is still JSON! – Shane Sepac Apr 08 '18 at 23:08
  • I'm sorry, I've been trying to figure this out for two days now. I am still trying to get TradePairId from https://www.cryptopia.co.nz/api/GetMarket/ETH_BTC But no matter what I do or what tutorial I follow I end up with errors. I am using org.json. How would I get it? I am currently trying to access "Data" in order to get to "TradePairId", but when I do Map data = ((Map)obj.get("Data")); I get a JSON null exception. – StealthVice7 Apr 10 '18 at 23:45
  • I also tried to follow the top rated answer here: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java But to no avail... – StealthVice7 Apr 10 '18 at 23:48
  • Neither does the following work: JSONObject obj = new JSONObject(IOUtils.toString(new URL("https://www.cryptopia.co.nz/api/GetMarket/" + currencyPair.toString()), Charset.forName("UTF-8"))); JSONObject data = obj.getJSONObject("Data"); despite similar code seemingly working in the link posted above. – StealthVice7 Apr 10 '18 at 23:54
  • @StealthVice7 Can you post a new question that includes the code and Ill look at it? – Shane Sepac Apr 12 '18 at 01:34
  • @Shn_Android_Dev I am blocked from asking new questions apparently, but I have pasted my code in the following link: https://pastebin.com/RzqCCXxt Thank you very much for your help! EDIT: currencyPair is ETH_BTC – StealthVice7 Apr 13 '18 at 01:44
  • @Shn_Android_Dev Hey, sorry it's been a while, I can't ask a new question still unfortunately... Can you help me out please? – StealthVice7 May 07 '18 at 01:04
0

For me, the better way to do this is using OkHttp to make the http requests and Gson to encode / decode the JSON objects.

There are a lot of libraries that can help you do this work.

Eduardo Folly
  • 146
  • 2
  • 6