0

Essentially, this is the kind of data I want returned:

{
   "Top10BidAsks":[
      {
         "Bid":{
            "Price":10.0,
            "Size":2.0,
            "ExchangeID":"SMART",
            "timeStamp":0
         },
         "Ask":{
            "Price":12.0,
            "Size":2.0,
            "ExchangeID":"SMART",
            "timeStamp":0
         }
      },
      {
         "Bid":{
            "Price":0.0,
            "Size":0.0,
            "ExchangeID":"SMART",
            "timeStamp":0
         },
         "Ask":{
            "Price":13.0,
            "Size":12.0,
            "ExchangeID":"SMART",
            "timeStamp":0
         }
      }
   ]
}

The {"Price":10.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0}, essentially represents a MarketData Object that I've created with those 4 fields.

The main function I'm calling is:

public MarketDataListLevel2 getMarketDataDepth() {
    try {
        MarketDataListLevel2 md = cs.getMarketDataDepth();
        log.info(md.toString());
        return md;
     }
     catch ( Exception e) {
        ....
     }
}

Where cs is just an interface that retrieves the JSON data from a site.

The MarketDataLevel2 object is:

public class MarketDataListLevel2 {

public static class MarketDataList {
    public MarketData[] a;
}

public MarketDataList[] listofmarketdatalists;

public MarketDataListLevel2(@JsonProperty("Top10BidAsks") MarketDataList[] listofmarketdatalists) {
    this.listofmarketdatalists = listofmarketdatalists;
}

I tried to make this object match the output I want (formatting wise), but apparently I might have a data structure error here, because I'm not getting the data I want returned.

When I run the first method that I listed:

MarketDataListLevel2 a = getDepthMarketData(coin);

When I debug this 'a' object, I see that each element in the listofmarketdatalists array is 'null' instead of containing an object with this format: {"Bid":{"Price":10.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0}, "Ask":{"Price":12.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0}}.

Any advice would be awesome.

1 Answer 1

1

You have to create correct POJO classes which represent your JSON. See below example:

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Root root = mapper.readValue(json, Root.class);
        System.out.println(root.getTop10());
    }
}

class Root {

    @JsonProperty("Top10BidAsks")
    private List<MarketDataEntity> top10;

    public List<MarketDataEntity> getTop10() {
        return top10;
    }

    public void setTop10(List<MarketDataEntity> top10) {
        this.top10 = top10;
    }
}

class MarketDataEntity {

    private Map<String, MarketData> datas = new HashMap<String, MarketData>();

    @JsonAnySetter
    public void setMarketData(String key, MarketData data) {
        datas.put(key, data);
    }

    @Override
    public String toString() {
        return datas.toString();
    }
}

class MarketData {

    @JsonProperty("Price")
    private BigDecimal price;

    @JsonProperty("Size")
    private BigDecimal size;

    @JsonProperty("ExchangeID")
    private String exchangeId;

    private int timeStamp;

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getSize() {
        return size;
    }

    public void setSize(BigDecimal size) {
        this.size = size;
    }

    public String getExchangeId() {
        return exchangeId;
    }

    public void setExchangeId(String exchangeId) {
        this.exchangeId = exchangeId;
    }

    public int getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(int timeStamp) {
        this.timeStamp = timeStamp;
    }

    @Override
    public String toString() {
        return "MarketData [price=" + price + ", size=" + size + ", exchangeId=" + exchangeId + ", timeStamp=" + timeStamp + "]";
    }
}

Above program prints:

[{Bid=MarketData [price=10.0, size=2.0, exchangeId=SMART, timeStamp=0], Ask=MarketData [price=12.0, size=2.0, exchangeId=SMART, timeStamp=0]}, {Bid=MarketData [price=0.0, size=0.0, exchangeId=SMART, timeStamp=0], Ask=MarketData [price=13.0, size=12.0, exchangeId=SMART, timeStamp=0]}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.