从JSON对象获取空数据

时间:2013-11-05 04:58:37

标签: java json jackson

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

{
   "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},   “询问”:{ “价格”:12.0, “大小” 为2.0, “交换ID”: “SMART”, “时间戳”:0}}。

任何建议都很棒。

1 个答案:

答案 0 :(得分: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]}]