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 Toolkit: https://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.
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.
- import json
- str = '{"name": "Bitcoin","price": 9346.5}'
- resultString = json.dumps(json.loads(str), indent=4, ensure_ascii=False)
- print(resultString)
use Maven GSON developed by google.
import GSON in pom.xml
code:<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></dependency>
- import com.google.gson.JsonElement;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonParser;
- //function
- public String format(String jsonStr) {
- JsonParser parser = new JsonParser();
- JsonElement je = parser.parse(jsonStr);
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- return gson.toJson(je);
- }
And that's how it's should be done.
No comments:
Post a Comment