xstream可以反序列化一个复杂的数组吗?

时间:2013-03-11 09:46:43

标签: java arrays json xstream

这些天我研究xstream。

但我发现xstream json教程在其主页上非常简单。 我有一个数组如下:

{
    "mails":[
                {
                    "uid":"ZC2027-mXOmcAtkfiztS0sEeJlkU25",
                    "relatedCardNums":"8299,0000,1531|8299,0000,1531",
                    "houseHolder":"",
                    "subject":"no-subject",
                    "receiveTime":"2012-05-27 00:00:00",
                    "bankName":"上海银行",
                    "cards":[]
                }
        ],
    "dealedNum":330,
    "nextRequestDelay":"1",
    "percent":"0",
    "filterNum":410,
    "resCode":"01",
    "dealedBillNum":43,
    "resMsg":"正在解析"
}

我想将此json字符串转换为GetMailsDataResponseDto,但我不知道该怎么办? 你能救我一下吗?

package com.fund.etrading.ebankapp.base.credit.cardniu.ecardniu.dto;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fund.etrading.ebankapp.base.credit.utils.FileUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public class GetMailsDataResponseDto extends ResponseBaseDto{
    protected int dealedNum;
    protected String nextRequestDelay;
    protected String percent;
    protected int filterNum;
    protected int dealedBillNum;

    protected List mails = new ArrayList();

    public List getMails() {
        return mails;
    }

    public int getDealedNum() {
        return dealedNum;
    }

    public String getNextRequestDelay() {
        return nextRequestDelay;
    }

    public String getPercent() {
        return percent;
    }

    public int getFilterNum() {
        return filterNum;
    }

    public int getDealedBillNum() {
        return dealedBillNum;
    }

    public void fromJson(String json){
        try {
            json = FileUtils.get_content("C:\\Documents and Settings\\Administrator\\workspace\\99fund_java\\src\\com\\fund\\etrading\\ebankapp\\base\\credit\\新建 文本文档 (2).txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        json = "{\"root\":" + json + "}";

        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.alias("root", this.getClass());

        //xstream.addImplicitCollection(this.getClass(), "mails");
        xstream.alias("mail", MailDto.class);

        //xstream.aliasField("cards", MailDto.class, "cards");
        //xstream.aliasField("currencyData", CardDto.class, "currencyData");
        //xstream.aliasField("data", CurrencyDataDto.class, "data");

        xstream.fromXML(json, this);
    }
}



package com.fund.etrading.ebankapp.base.credit.cardniu.ecardniu.dto;

import java.util.ArrayList;
import java.util.List;

import com.fund.etrading.ebankapp.base.credit.BaseDto;

public class MailDto extends BaseDto{
    protected String uid;
    protected String relatedCardNums;
    protected String houseHolder;
    protected String subject;
    protected String receiveTime;
    protected String bankName;

    protected List cards = new ArrayList();

    public String getUid() {
        return uid;
    }

    public String getRelatedCardNums() {
        return relatedCardNums;
    }

    public String getHouseHolder() {
        return houseHolder;
    }

    public String getSubject() {
        return subject;
    }

    public String getReceiveTime() {
        return receiveTime;
    }

    public String getBankName() {
        return bankName;
    }

    public List getCards() {
        return cards;
    }
}

提前感谢!

2 个答案:

答案 0 :(得分:-1)

如果您想将json字符串转换为自定义类(ex.GetMailsDataResponseDto),我建议使用Google Gson。 如果你使用Gson,那么在GetMailsDataResponseDto类中不需要fromJosn()方法。

如果您只使用json解析并具有java脚本的经验,我推荐Djson解析器(java库)。

答案 1 :(得分:-1)

“Djson Parse版本0.8a” - http://blog.indf.net/category/Apps/djson

j1.txt - 提示:“无BOM& UTF-8”

....
   public void fromJson(String json){
        //(real-code)--start
        //Var var = Djson.parse(json);
        //(real-code)--end

        //--test-code--start
        Var var = null;
        try {
            var = Djson.parse(new File("d:\\j1.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //--test-code--end

        this.dealedNum        = var.get("dealedNum").toInt();
        this.nextRequestDelay = var.get("nextRequestDelay").toString();
        this.percent          = var.get("percent").toString();
        this.filterNum        = var.get("filterNum").toInt();
        this.dealedBillNum  = var.get("dealedBillNum").toInt();

        for(int i=0; i<var.get("mails").size(); i++) {
            this.mails.add(var.get("mails").get(i).toObject()); // MAP type setting...
        }       
    }