Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

java - Convenient way to extract data from the MtGox/PubNub JSON API?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

PubNub Java Class for MtGox JSON API

It's easy to create a ready made Java Class for ingesting the live feed provided by Mt.Gox This is a work-in-progress post to show you how to access the PubNub Data Feed from Mt.Gox as shown in the Dev Console live feed!

Official Bitcoin Wiki JSON Streaming API

We will be working from the Bitcoin wiki feed instructions provided by Bitcoin official Wiki: https://en.bitcoin.it/wiki/MtGox/API/Pubnub - continue reading below the screenshot to continue.

PubNub Developer Console

To see the live real-time data feed we will be using, please checkout the following two links:

  1. Live Feed Trade Events (Buy/Sell Feed): https://www.pubnub.com/console?sub=sub-c-50d56e1e-2fd9-11e3-a041-02ee2ddab7fe&pub=demo&channel=dbf1dee9-4f2e-4a08-8cb7-748919a71b21&origin=pubsub.pubnub.com&ssl=true
  2. Live Feed Ticker Updates (Price Changes): https://www.pubnub.com/console?sub=sub-c-50d56e1e-2fd9-11e3-a041-02ee2ddab7fe&pub=demo&channel=d5f06780-30a8-4a48-a2f8-7ed181b4a13f&origin=pubsub.pubnub.com&ssl=true
  3. Trade Lag Example: https://www.mtgox.com/lag.html

PubNub Java SDK Docs

We will be using the PubNub Java SDK Docs http://www.pubnub.com/docs/java/javase/overview/data-push.html

Specifically we'll be using the mtgox.subcribe(...) instance method to focus our efforts which looks like the following:

Download JAR or Source: https://github.com/pubnub/mtgox

import org.json.JSONObject;
import com.pubnub.mtgox.MtGox;
import com.pubnub.mtgox.MtGoxCallback;

public class PubnubMtGoxSample {

    public static void main(String[] args) {
        MtGox mtgx = new MtGox();

        mtgx.subscribe("ticker.BTCUSD", new MtGoxCallback(){

            @Override
            public void callback(JSONObject data) {
                try {
                    String channel_name = data.getString("channel_name");
                    String avg_value = data.getJSONObject("ticker").getJSONObject("avg").getString("value");
                    System.out.println(channel_name + " : " + avg_value);
                } catch (Exception e) {}

            }});
    }
}

See Full MtGox Example with Java Source Code - https://github.com/pubnub/mtgox/blob/master/java/examples/PubnubMtGoxSample.java

To compile the example got to https://github.com/pubnub/mtgox/tree/master/java and run

javac -cp Pubnub-MtGox.jar:libs/json-20090211.jar   examples/PubnubMtGoxSample.java

And then to RUN:

java -cp .:examples/:Pubnub-MtGox.jar:Pubnub-StandardEdition-3.5.6.jar:libs/json-20090211.jar:libs/bcprov-jdk15on-1.47.jar:libs/slf4j-api-1.7.5.jar:libs/slf4j-nop-1.7.5.jar PubnubMtGoxSample

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...