2

I'm using the Pubnub API with Java for pulling data from MtGox.

I would like to do the following, where Message is the 3rd party class that I'm looking for:

public void successCallback(String channel, Object message) {
    JSONObject messageJson = (JSONObject) message;

    // This next line is where I'm stuck
    ObjectMapper mapper = new ObjectMapper(); 
    Message myMessage = mapper.readValue(messageJson.toString(), Message.class);

    // do stuff with myMessage here
}

How to read out the result of the JSON response in a way I can use in my code?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Daniel S.
  • 6,458
  • 4
  • 35
  • 78
  • 2
    We are building a response to this answer and will provide a full SDK that will be hosted here on the answer forum with the API. – Stephen Blum Dec 12 '13 at 04:09
  • Do you have specific requirements for this Ready-Made Java Class as we put one together? – Stephen Blum Dec 12 '13 at 04:19
  • @PubNub, please cf. http://stackoverflow.com/questions/20534862/how-to-efficiently-map-a-org-json-jsonobject-to-a-pojo about a closely related topic. – Daniel S. Dec 12 '13 at 04:36
  • Something about this question-and-answer tandem feels kind of off, so I've [opened a Meta discussion about it](http://meta.stackexchange.com/q/211204/175248). Gut feeling: close as asking for third-party resources, but I'm not 100% sure. – Makoto Dec 12 '13 at 04:59
  • 2
    @Makoto yeah, this is OT as it stands originally. However, the answer is worth tweaking the question to remove the outright request for links/libs. I've edited to do so, hopefully successfully. –  Dec 12 '13 at 14:36
  • Editors, I appreciate your efforts to enhance this question and I like my question to be edited. But please be aware that 1. there is a meta discussion about this question, which might become more difficult if this question is edited, and 2. Now the final question in the end does not anymore reflect my original problem. I do know a way how to access the information in the JSON object. I just don't know a convenient way. Moreover, the final question does not anymore line up with the description near the beginning. – Daniel S. Dec 12 '13 at 15:39
  • I've opened a new question which is compliant with SO's requirements here: http://stackoverflow.com/questions/20547815/java-is-there-a-convenient-way-to-extract-data-from-the-mtgox-pubnub-json-api . I think we should revert the old question here to the state before the edits, for reference for the meta-question, and lock it or close it, and continue commenting and answering the original topic in the new question. – Daniel S. Dec 12 '13 at 15:45

1 Answers1

1

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
Stephen Blum
  • 6,498
  • 2
  • 34
  • 46