0

本质上,这是我想要返回的数据类型:

{
   "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
         }
      }
   ]
}

{"Price":10.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0} 本质上表示我使用这 4 个字段创建的 MarketData 对象。

我调用的主要功能是:

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

其中 cs 只是一个从站点检索 JSON 数据的接口。

MarketDataLevel2 对象是:

public class MarketDataListLevel2 {

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

public MarketDataList[] listofmarketdatalists;

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

我试图使这个对象与我想要的输出匹配(格式明智),但显然我在这里可能有一个数据结构错误,因为我没有得到我想要返回的数据。

当我运行我列出的第一个方法时:

MarketDataListLevel2 a = getDepthMarketData(coin);

当我调试这个“a”对象时,我看到 listofmarketdatalists 数组中的每个元素都是“null”,而不是包含具有以下格式的对象:{"Bid":{"Price":10.0,"Size":2.0," ExchangeID":"SMART","timeStamp":0}, "Ask":{"Price":12.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0}}。

任何建议都会很棒。

4

1 回答 1

1

您必须创建代表 JSON 的正确 POJO 类。请参见下面的示例:

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 + "]";
    }
}

上面的程序打印:

[{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]}]
于 2013-11-05T08:53:02.177 回答