这个概念
对我来说,最好的解决方案是将s 从 PubNub API 转换为我在XChange 库JSONObject
的 MtGox 模块中找到的 bean 类。
承认,这种方法添加了相当多的胶水代码,正如您在这个答案的末尾看到的那样,但我认为这是值得的,因为在转换之后,代码变得更加简单。例如,要从 BTC 最后交易的股票代码中获取汇率和货币,您可以简单地写
ticker.getLast().getValue()
和
ticker.getLast().getCurrency()
怎么做
XChange 库的mtgox 模块作为 maven artifact 提供,非常方便。您只需将此模块作为依赖项添加到您的项目中,项目设置就完成了。
在 xchange-mtgox 模块中,您将找到com.xeiam.xchange.mtgox.v2.dto.marketdata包,其中包含两个类MtGoxTrade
和MtGoxTicker
.
JSONObject
使用 Jackson 可以轻松地从这些课程转换为其中一个课程ObjectMapper
。作为一个优势,Jackson 库作为 xchange-mtgox maven 工件的传递依赖项自动导入。这意味着,如果您使用的是 maven,您甚至不必编写一行代码即可将其添加到您的项目中。
下面是一个完整的可运行示例。大多数是使用 PubNub 的标准代码。重要的位在标记// BEGIN OF IMPORTANT PART
和之间// END OF IMPORTANT PART
。
public class PubNubMtGoxBeanExample {
private static final String MTGOXPUBNUB_SUBSCRIBE_KEY = "sub-c-50d56e1e-2fd9-11e3-a041-02ee2ddab7fe";
private static final String MTGOXPUBNUB_BTCEUR_CHANNEL = "0bb6da8b-f6c6-4ecf-8f0d-a544ad948c15";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static void main(String[] args) throws PubnubException {
Pubnub pubnub = new Pubnub("demo", MTGOXPUBNUB_SUBSCRIBE_KEY);
pubnub.subscribe(MTGOXPUBNUB_BTCEUR_CHANNEL, new Callback() {
@Override
public void successCallback(String channel, Object message) {
// BEGIN OF IMPORTANT PART
JSONObject messageJson = (JSONObject) message;
JSONObject tickerJson;
try {
tickerJson = messageJson.getJSONObject("ticker");
} catch (JSONException e) {
throw new RuntimeException(e);
}
MtGoxTicker ticker;
try {
// the following line is the most important, because it maps from the JSONObject to the MtGoxTicker class
ticker = OBJECT_MAPPER.readValue(tickerJson.toString(), MtGoxTicker.class);
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
String currency = ticker.getLast().getCurrency();
BigDecimal value = ticker.getLast().getValue();
System.out.println(currency + " " + value);
// END OF IMPORTANT PART
}
@Override
public void connectCallback(String channel, Object message) {
System.out.println("connectCallback on channel:" + channel + " : " + message.getClass() + " : " + message.toString());
}
@Override
public void disconnectCallback(String channel, Object message) {
System.out.println("disconnectCallback on channel:" + channel + " : " + message.getClass() + " : " + message.toString());
}
@Override
public void reconnectCallback(String channel, Object message) {
System.out.println("reconnectCallback on channel:" + channel + " : " + message.getClass() + " : " + message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
System.out.println("errorCallback on channel " + channel + " : " + error.toString());
}
});
}
}
为清楚起见,我删除了导入,您可以在大多数 IDE 中使用适当的热键将其重新添加(在 Eclipse 中为 Ctrl+Shift+O)。
此外,请注意,此代码中存在性能损失,可以通过遵循问题
如何有效地将 org.json.JSONObject 映射到 POJO?