1

这些天我学习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;
    }
}

提前致谢!

4

2 回答 2

-1

If you want to convert json string to your custom class(ex.GetMailsDataResponseDto), I recommend Google Gson. If you use Gson, yon don't need fromJosn() method in GetMailsDataResponseDto class.

If you only use json parsing and have experiences of java script, I recommend Djson parser(java library).

于 2013-03-12T01:24:48.530 回答
-1

“Djson 解析版本 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...
        }       
    }
于 2013-03-12T03:29:09.000 回答