下面列出了com.alibaba.fastjson.JSONArray#getLong ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 解析订单详情
* [
* 333168795, ID integer Trade database id
* "tLTCUSD", PAIR string Pair (BTCUSD, …)
* 1547796266000, MTS_CREATE integer Execution timestamp
* 21597693840, ORDER_ID integer Order id
* -0.4, EXEC_AMOUNT float Positive means buy, negative means sell
* 31.962, EXEC_PRICE float Execution price
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* -1, MAKER int 1 if true, -1 if false
* -0.0255696, FEE float Fee
* "USD" FEE_CURRENCY string Fee currency
* ]
* @param result 元素数据
* @return 订单详情列表
*/
public static List<OrderDetail> parseOrderDetails(String result) {
JSONArray r = JSON.parseArray(result);
List<OrderDetail> details = new ArrayList<>(r.size());
for (int i = 0; i < r.size(); i++) {
JSONArray t = r.getJSONArray(i);
String data = r.getString(i);
Long time = t.getLong(2);
String orderId = t.getString(3);
String detailId = t.getString(0);
BigDecimal price = t.getBigDecimal(5);
BigDecimal amount = t.getBigDecimal(4).abs();
BigDecimal fee = t.getBigDecimal(9).abs();
String feeCurrency = t.getString(10);
details.add(new OrderDetail(data, time, orderId, detailId, price, amount, fee, feeCurrency));
}
return details;
}
@Override
protected Klines transformKlines(List<String> results, ExchangeInfo info) {
String result = results.get(0);
if (useful(result)) {
JSONArray array = JSON.parseArray(result);
List<Kline> klines = new ArrayList<>(array.size());
for (int i = array.size() - 1; 0 <= i; i--) {
JSONArray t = array.getJSONArray(i);
/*
* 1499040000000, // Open time
* "0.01634790", // Open
* "0.80000000", // High
* "0.01575800", // Low
* "0.01577100", // Close
* "148976.11427815", // Volume
* 1499644799999, // Close time
* "2434.19055334", // Quote asset volume
* 308, // Number of trades
* "1756.87402397", // Taker buy base asset volume
* "28.46694368", // Taker buy quote asset volume
* "17928899.62484339" // Ignore.
*/
Long time = t.getLong(0) / 1000;
Kline kline = CommonUtil.parseKlineByIndex(array.getString(i), t, time, 1);
klines.add(kline);
// if (size <= records.size()) break;
}
return new Klines(klines);
}
return null;
}
static Order of(JSONArray r) {
return new Order(
Util.decode(r.getString(0)),
r.getLong(1),
r.getString(2),
Util.of(r.getString(3), Side::valueOf),
Util.of(r.getString(4), Type::valueOf),
Util.of(r.getString(5), State::valueOf),
r.getBigDecimal(6),
r.getBigDecimal(7),
r.getBigDecimal(8),
r.getBigDecimal(9));
}
static OrderDetail of(JSONArray r) {
return new OrderDetail
(Util.decode(r.getString(0)),
r.getLong(1),
r.getString(2),
r.getString(3),
r.getBigDecimal(4),
r.getBigDecimal(5),
r.getBigDecimal(6),
r.getString(7),
Util.of(r.getString(8), Side::valueOf));
}
static Trade of(JSONArray r) {
return new Trade(
Util.decode(r.getString(0)),
r.getLong(1),
r.getString(2),
Util.of(r.getString(3), Side::valueOf),
r.getBigDecimal(4),
r.getBigDecimal(5));
}
static Kline of(JSONArray r) {
return new Kline(
Util.decode(r.getString(0)),
r.getLong(1),
r.getBigDecimal(2),
r.getBigDecimal(3),
r.getBigDecimal(4),
r.getBigDecimal(5),
r.getBigDecimal(6));
}
@Override
public List<Message> format(ByteBuf payload) {
if (payload == null) {
return null;
}
String txt = payload.toString(StandardCharsets.UTF_8);
JSONObject jsonObject = JSON.parseObject(txt);
Object timestamp = jsonObject.get(JSON_KEY_TIMESTAMP);
if (timestamp != null) {
return Lists.newArrayList(JSON.parseObject(txt, Message.class));
}
String device = jsonObject.getString(JSON_KEY_DEVICE);
JSONArray timestamps = jsonObject.getJSONArray(JSON_KEY_TIMESTAMPS);
JSONArray measurements = jsonObject.getJSONArray(JSON_KEY_MEASUREMENTS);
JSONArray values = jsonObject.getJSONArray(JSON_KEY_VALUES);
List<Message> ret = new ArrayList<>();
for (int i = 0; i < timestamps.size(); i++) {
Long ts = timestamps.getLong(i);
Message message = new Message();
message.setDevice(device);
message.setTimestamp(ts);
message.setMeasurements(measurements.toJavaList(String.class));
message.setValues(((JSONArray)values.get(i)).toJavaList(String.class));
ret.add(message);
}
return ret;
}
/**
* 判断一个值是否存在于一个JSONArray对象中.
* 为了修复JSONArray自带contains方法的Bug.
* 使用场景: 判断一个用户的UID是否存在于Vote列表中.
* @param jsonArray - 待判断的JSONArray对象
* @param value - 待检查的值
* @return 一个值是否存在于一个JSONArray对象中
*/
private boolean contains(JSONArray jsonArray, long value) {
for ( int i = 0; i < jsonArray.size(); ++ i ) {
if ( jsonArray.getLong(i) == value ) {
return true;
}
}
return false;
}
/**
* 移除JSONArray对象中的一个值.
* 为了修复JSONArray自带contains方法的Bug.
* 使用场景: 从Vote列表中移除某个用户的UID.
* @param jsonArray - 待移除值的JSONArray对象
* @param value - 待移除的值
*/
private void remove(JSONArray jsonArray, long value) {
for ( int i = 0; i < jsonArray.size(); ++ i ) {
if ( jsonArray.getLong(i) == value ) {
jsonArray.remove(i);
}
}
}
@Override
protected Klines transformKlines(List<String> results, ExchangeInfo info) {
String result = results.get(0);
if (useful(result)) {
JSONArray array = JSON.parseArray(result);
List<Kline> klines = new ArrayList<>(array.size());
long step = info.getPeriod().getValue();
Long lastTime = null;
BigDecimal lastClose = null;
// new ... old
for (int i = array.size() - 1; 0 <= i; i--) {
JSONArray t = array.getJSONArray(i);
/*
[
MTS 1364824380000,
OPEN 99.01,
CLOSE 99.01,
HIGH 99.01,
LOW 99.01,
VOLUME 6
]
*/
Long time = t.getLong(0) / 1000;
if (null != lastTime) {
while (lastTime + step < time) {
//
lastTime = lastTime + step;
klines.add(new Kline(String.format("[%s,%s,%s,%s,%s,%s]",
lastTime * 1000, lastClose, lastClose, lastClose, lastClose, 0),
lastTime, lastClose, lastClose, lastClose, lastClose, BigDecimal.ZERO));
}
}
Kline kline = CommonUtil.parseKlineByIndex(result, t, time, 1);
klines.add(kline);
lastTime = time;
lastClose = kline.getClose();
}
Collections.reverse(klines);
return new Klines(klines);
}
return null;
}
/**
* [
* 21597693840, ID int64 Order ID
* null, GID int Group ID
* 26665933660, CID int Client Order ID
* "tLTCUSD", SYMBOL string Pair (tBTCUSD, …)
* 1547796266000, MTS_CREATE int Millisecond timestamp of creation
* 1547796266000, MTS_UPDATE int Millisecond timestamp of update
* 0, AMOUNT float Remaining amount.
* -0.4, AMOUNT_ORIG float Original amount, positive means buy, negative means sell.
* "EXCHANGE LIMIT", TYPE string The type of the order: LIMIT, MARKET, STOP, TRAILING STOP, EXCHANGE MARKET, EXCHANGE LIMIT, EXCHANGE STOP, EXCHANGE TRAILING STOP, FOK, EXCHANGE FOK.
* null, TYPE_PREV string Previous order type
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* "0", FLAGS int Upcoming Params Object (stay tuned)
* "EXECUTED @ 31.962(-0.4)", ORDER_STATUS string Order Status: ACTIVE, EXECUTED, PARTIALLY FILLED, CANCELED
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* 30, PRICE float Price
* 31.962, PRICE_AVG float Average price
* 0, PRICE_TRAILING float The trailing price
* 0, PRICE_AUX_LIMIT float Auxiliary Limit price (for STOP LIMIT)
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* 0, NOTIFY int 1 if Notify flag is active, 0 if not
* 0, HIDDEN int 1 if Hidden, 0 if not hidden
* null, PLACED_ID int If another order caused this order to be placed (OCO) this will be that other order's ID
* null,
* null,
* "API>BFX",
* null,
* null,
* null
* ]
* @param result 原始数据
* @return 订单列表
*/
public static List<Order> parseOrders(String result) {
JSONArray r = JSON.parseArray(result);
List<Order> orders = new ArrayList<>(r.size());
for (int i = 0; i < r.size(); i++) {
JSONArray t = r.getJSONArray(i);
String data = r.getString(i);
Long time = t.getLong(4);
String id = t.getString(0);
Side side = null;
Double s = t.getDouble(7);
if (0 < s) {
side = Side.BUY;
} else if (s < 0) {
side = Side.SELL;
}
Type type = null;
String orderType = t.getString(8);
if ("EXCHANGE LIMIT".equals(orderType)) {
type = Type.LIMIT;
} else if ("EXCHANGE MARKET".equals(orderType)) {
type = Type.MARKET;
} else if ("EXCHANGE FOK".equals(orderType)) {
type = Type.FILL_OR_KILL;
}
State state = null;
String orderState = t.getString(13);
if (orderState.contains("ACTIVE")) {
state = State.SUBMIT;
} else if (orderState.contains("EXECUTED")) {
state = State.FILLED;
} else if (orderState.contains("PARTIALLY FILLED")) {
state = State.UNDONE;
} else if (orderState.contains("CANCELED")) {
state = State.CANCEL;
}
BigDecimal price = t.getBigDecimal(16);
BigDecimal amount = t.getBigDecimal(7).abs();
BigDecimal deal = amount.subtract(t.getBigDecimal(6).abs());
BigDecimal average = t.getBigDecimal(17);
if (state == State.SUBMIT && greater(deal, BigDecimal.ZERO)) {
state = State.PARTIAL;
}
if (state == State.CANCEL && greater(deal, BigDecimal.ZERO)) {
state = State.UNDONE;
}
orders.add(new Order(data, time, id, side, type, state, price, amount, deal, average));
}
return orders;
}