把取得的json对象转换成javabean对象

JSON对象如下:
{"EURUSD":{"code":"EURUSD","close":1.2913,"open":1.2897,"high":1.2957,"low":1.2861,"point_price":1,"lasttime":"2009-03-14 03:59:00","lasttimestamp":1236974340},"GBPUSD":{"code":"GBPUSD","close":1.39992,"open":1.39309,"high":1.4069,"low":1.38639,"point_price":1,"lasttime":"2009-03-14 04:59:00","lasttimestamp":1236977940},"USDJPY":{"code":"USDJPY","close":98.02,"open":97.31,"high":98.65,"low":97.13,"point_price":1.0202,"lasttime":"2009-03-14 03:59:00","lasttimestamp":1236974340},"USDCHF":{"code":"USDCHF","close":1.1876,"open":1.1872,"high":1.1934,"low":1.1835,"point_price":0.84203,"lasttime":"2009-03-14 03:59:00","lasttimestamp":1236974340},"USDCAD":{"code":"USDCAD","close":1.2725,"open":1.2794,"high":1.2842,"low":1.2625,"point_price":0.78585,"lasttime":"2009-03-14 03:59:00","lasttimestamp":1236974340},"AUDUSD":{"code":"AUDUSD","close":0.6573,"open":0.6541,"high":0.6605,"low":0.6517,"point_price":1,"lasttime":"2009-03-14 03:59:00","lasttimestamp":1236974340},"NZDUSD":{"code":"NZDUSD","close":0.5248,"open":0.52051,"high":0.52671,"low":0.51828,"point_price":1,"lasttime":"2009-03-14 04:59:00","lasttimestamp":1236977940}}

javabean对象

package bean;

import java.io.Serializable;
import java.math.BigDecimal;

public class RateInfo implements Serializable {

    private BigDecimal open;
    private String lasttime;
    private BigDecimal high;
    private String code;
    private BigDecimal low;
    private String lasttimestamp;
    private BigDecimal close;
    private BigDecimal point_price;
    public BigDecimal getOpen() {
        return open;
    }
    public void setOpen(BigDecimal open) {
        this.open = open;
    }
    public String getLasttime() {
        return lasttime;
    }
    public void setLasttime(String lasttime) {
        this.lasttime = lasttime;
    }
    public BigDecimal getHigh() {
        return high;
    }
    public void setHigh(BigDecimal high) {
        this.high = high;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public BigDecimal getLow() {
        return low;
    }
    public void setLow(BigDecimal low) {
        this.low = low;
    }
    public String getLasttimestamp() {
        return lasttimestamp;
    }
    public void setLasttimestamp(String lasttimestamp) {
        this.lasttimestamp = lasttimestamp;
    }
    public BigDecimal getClose() {
        return close;
    }
    public void setClose(BigDecimal close) {
        this.close = close;
    }
    public BigDecimal getPoint_price() {
        return point_price;
    }
    public void setPoint_price(BigDecimal point_price) {
        this.point_price = point_price;
    }

}


代码如下:

            JSONObject jsonObj = JSONObject.fromObject(rate);

            for (Iterator iterator = jsonObj.keySet().iterator(); iterator
                    .hasNext();) {
                String key = (String) iterator.next();
                JSONObject jsonValue = jsonObj.getJSONObject(key);
                RateInfo rateInfo = (RateInfo) JSONObject.toBean(jsonValue,
                        RateInfo.class);
                logger.debug(ToStringBuilder.reflectionToString(rateInfo));
                rateInfos.put(rateInfo.getCode(),rateInfo);
            }


采用的json库是json-lib库,该库还需要

  • jakarta commons-lang 2.4
  • jakarta commons-beanutils 1.7.0
  • jakarta commons-collections 3.2
  • jakarta commons-logging 1.1.1
  • ezmorph 1.0.6

你可能感兴趣的:(json,bean)