I'm using the PubNub API with Java for pulling data from MtGox.
When retrieving data, the API delivers it in form of a JSONObject
, which represents a tree structure of JSON data. Trying to extract bits of information directly from a JSONObject
produces ugly code with lots of calls to getJSONObject(String)
, for which again exceptions might need to be handled.
Therefor, I'm looking for a convenient way to extract information from the JSONObject
responses. So far, I've come across the possibility to convert the JSONObject
into a POJO and then access the POJO. For conversion, I found the ObjectMapper
from the Jackson library, which does a nice job here:
public void successCallback(String channel, Object message) {
JSONObject messageJson = (JSONObject) message;
ObjectMapper mapper = new ObjectMapper();
Message myMessage = mapper.readValue(messageJson.toString(), Message.class);
// do stuff with myMessage here
}
This approach has the disadvantage that I have to write my own POJO classes, e.g. the Message class in the above example, because I could not find these classes ready to use anywhere.
How to conveniently access the information stored in the JSONObject
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…