| | |
| | | |
| | | import javax.websocket.*; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.net.URI; |
| | | import java.nio.ByteBuffer; |
| | | import java.util.Arrays; |
| | |
| | | |
| | | // 确保 dataNode 不为 null,以避免空指针异常 |
| | | if (dataNode != null) { |
| | | // 存储数据到 HashMap |
| | | Map<String, Object> bidAskMap = new HashMap<>(); // 变量命名更具描述性 |
| | | bidAskMap.put("bids", dataNode.get("bids")); // 获取并存储 bids |
| | | bidAskMap.put("asks", dataNode.get("asks")); // 获取并存储 asks |
| | | Map<String, Object> hashMap = new HashMap<>(); // 变量命名更具描述性 |
| | | |
| | | Map<String, Object> argMap = gson.fromJson(map.get("arg").toString(), new TypeToken<Map<String, Object>>() {}.getType()); // 解析 arg 为 Map |
| | | String key = "bitget" + argMap.get("instId"); // 构建 Redis 存储的键 |
| | | String bids = dataNode.get("bids").toString(); |
| | | String asks = dataNode.get("asks").toString(); |
| | | |
| | | // 存储到 Redis,使用 ObjectMapper 转换为 JSON 字符串 |
| | | String jsonData = objectMapper.writeValueAsString(bidAskMap); // 先将 HashMap 转换为 JSON 字符串 |
| | | RedisUtil.set(key, jsonData); // 存储数据 |
| | | Type listType = new TypeToken<List<List<String>>>(){}.getType(); |
| | | List<List<String>> bidsList = gson.fromJson(bids, listType); |
| | | List<List<String>> asksList = gson.fromJson(asks, listType); |
| | | |
| | | if(!bidsList.isEmpty() && !bidsList.get(0).isEmpty() && !asksList.isEmpty() && !asksList.get(0).isEmpty()){ |
| | | if (!bidsList.isEmpty() && !bidsList.get(0).isEmpty()) { |
| | | List<String> bidsStringList = bidsList.get(0); |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | pvMap.put("p", new BigDecimal(bidsStringList.get(0)).toPlainString()); |
| | | pvMap.put("v", new BigDecimal(bidsStringList.get(1)).toPlainString()); |
| | | hashMap.put("bids", pvMap); // 获取并存储 bids |
| | | } |
| | | |
| | | if (!asksList.isEmpty() && !asksList.get(0).isEmpty()) { |
| | | List<String> asksStringList = asksList.get(0); // 修正此处为 asksList |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | pvMap.put("p", new BigDecimal(asksStringList.get(0)).toPlainString()); |
| | | pvMap.put("v", new BigDecimal(asksStringList.get(1)).toPlainString()); |
| | | hashMap.put("asks", pvMap); // 获取并存储 asks |
| | | } |
| | | |
| | | Map<String, Object> argMap = gson.fromJson(map.get("arg").toString(), new TypeToken<Map<String, Object>>() {}.getType()); // 解析 arg 为 Map |
| | | String key = "bitget" + argMap.get("instId"); // 构建 Redis 存储的键 |
| | | |
| | | // 存储到 Redis,使用 ObjectMapper 转换为 JSON 字符串 |
| | | String jsonData = objectMapper.writeValueAsString(hashMap); // 先将 HashMap 转换为 JSON 字符串 |
| | | RedisUtil.set(key, jsonData); // 存储数据 |
| | | } |
| | | } |
| | | } |
| | | } catch (JsonSyntaxException e) { |