Thursday, May 10, 2018

How to use CoinMarketCap JSON API

CoinMarketCap is a professional blockchain market information website. You can get real time market information from there. Such as Maret cap, trade volume, trending and so on.
If you use API provided by coinmarketcap, you can build your own version of coinmarketcap. How can you use this API, let's find out.

Step 1, you need to visit this API list https://coinmarketcap.com/api/.
Step 2, As above, if you visit https://api.coinmarketcap.com/v2/ticker/?limit=10 in your browser, you can get the raw json string.


Step 3, you can use a online json format tools to minify the data.
such as Best Programming Toolkithttps://bestprogrammingtoolkit.com/json-formatter
put the json string you just get in step 2 in the first textbox, then click "Compact/Minify".
the result will show in the second textbox.

FYI, Best Programming Toolkit is a very useful website, it can do many task such as:
JSON formatter: https://bestprogrammingtoolkit.com/json-formatter
XML formatter: https://bestprogrammingtoolkit.com/xml-formatter


Step 4, use Java or json or any programming language to parse the string you got in the step 3.
In python:
use build-in package JSON.
  1. import json
  2. str = '{"name": "Bitcoin","price": 9346.5}'
  3. resultString = json.dumps(json.loads(str), indent=4, ensure_ascii=False)
  4. print(resultString)
In Java:
use Maven GSON developed by google.
import GSON in pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
code:
  1. import com.google.gson.JsonElement;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.JsonParser;
  5. //function
  6. public String format(String jsonStr) {
  7. JsonParser parser = new JsonParser();
  8. JsonElement je = parser.parse(jsonStr);
  9. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  10. return gson.toJson(je);
  11. }


And that's how it's should be done.

No comments:

Post a Comment