0

我有一个包含这些值的字符串:

String verifyPaymentDetails = "{
2298597={mihpayid=403993715508098532, request_id=NULL, bank_ref_num=NULL, amt=53.77, disc=0.00,   mode=CC, PG_TYPE=AXIS, card_no=512345XXXXXX2346, name_on_card=emu, udf2=0, addedon=2013-06-03 17:34:42, status=failure, unmappedstatus=failed, Merchant_UTR=NULL, Settled_At=NULL}, 
6503939={mihpayid=Not Found, status=Not Found}
}"

我想从上面的字符串中提取值并将它们存储如下:

键将是2298597键的值mihpayid=403993715508098532, request_id=NULL, bank_ref_num=NULL, amt=53.77, disc=0.00, mode=CC, PG_TYPE=AXIS, card_no=512345XXXXXX2346, name_on_card=emu, udf2=0, addedon=2013-06-03 17:34:42, status=failure, unmappedstatus=failed, Merchant_UTR=NULL, Settled_At=NULL

Map<String,Item> tag = new HashMap<String,VerifyPaymentRO>();

这就是我的VerifyPaymentRO

public class VerifyPaymentRO {
private String mihpayid;
private String request_id;
private String bank_ref_num;
private String amt;
private String disc;
private String mode;
private String PG_TYPE;
private String card_no;
private String name_on_card;
private String udf2;
private String addedon;
private String status;
private String unmappedstatus;
private String Merchant_UTR;
private String Settled_At;

public String getMihpayid() {
    return mihpayid;
}
public void setMihpayid(String mihpayid) {
    this.mihpayid = mihpayid;
}
public String getRequest_id() {
    return request_id;
}
public void setRequest_id(String request_id) {
    this.request_id = request_id;
}
public String getBank_ref_num() {
    return bank_ref_num;
}
public void setBank_ref_num(String bank_ref_num) {
    this.bank_ref_num = bank_ref_num;
}
public String getAmt() {
    return amt;
}
public void setAmt(String amt) {
    this.amt = amt;
}
public String getDisc() {
    return disc;
}
public void setDisc(String disc) {
    this.disc = disc;
}
public String getMode() {
    return mode;
}
public void setMode(String mode) {
    this.mode = mode;
}
public String getPG_TYPE() {
    return PG_TYPE;
}
public void setPG_TYPE(String pG_TYPE) {
    PG_TYPE = pG_TYPE;
}
public String getCard_no() {
    return card_no;
}
public void setCard_no(String card_no) {
    this.card_no = card_no;
}
public String getName_on_card() {
    return name_on_card;
}
public void setName_on_card(String name_on_card) {
    this.name_on_card = name_on_card;
}
public String getUdf2() {
    return udf2;
}
public void setUdf2(String udf2) {
    this.udf2 = udf2;
}
public String getAddedon() {
    return addedon;
}
public void setAddedon(String addedon) {
    this.addedon = addedon;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getUnmappedstatus() {
    return unmappedstatus;
}
public void setUnmappedstatus(String unmappedstatus) {
    this.unmappedstatus = unmappedstatus;
}
public String getMerchant_UTR() {
    return Merchant_UTR;
}
public void setMerchant_UTR(String merchant_UTR) {
    Merchant_UTR = merchant_UTR;
}
public String getSettled_At() {
    return Settled_At;
}
public void setSettled_At(String settled_At) {
    Settled_At = settled_At;
}

}

那么如何从字符串中获取 id 和 id 的值并将值复制到 POJO 并将它们像 id 和 object 一样存储在 HashMap 中?

如果春天有办法也可以

4

1 回答 1

0

假设它不是 JSON 并且您确实需要做艰苦的工作,我认为最好的解决方案是使用commons-beanutils中的RojoBeanMap的组合。我也是 Rojo 的作者,所以我想展示如何在这种情况下使用它。由于内部对数的动态变化,我不建议按组直接进行正则表达式映射,这就是为什么我认为 BeanMap 可以为我们做很多工作的原因。

首先让我们用id更新你的 bean ,它将作为主键:

public class VerifyPaymentRO {
    private String id;

    private String mihpayid;
    private String request_id;
    private String bank_ref_num;
    private String amt;
    private String disc;
    private String mode;
    private String PG_TYPE;
    private String card_no;
    private String name_on_card;
    private String udf2;
    private String addedon;
    private String status;
    private String unmappedstatus;
    private String Merchant_UTR;
    private String Settled_At;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMihpayid() {
        return mihpayid;
    }

    public void setMihpayid(String mihpayid) {
        this.mihpayid = mihpayid;
    }

    public String getRequest_id() {
        return request_id;
    }

    public void setRequest_id(String request_id) {
        this.request_id = request_id;
    }

    public String getBank_ref_num() {
        return bank_ref_num;
    }

    public void setBank_ref_num(String bank_ref_num) {
        this.bank_ref_num = bank_ref_num;
    }

    public String getAmt() {
        return amt;
    }

    public void setAmt(String amt) {
        this.amt = amt;
    }

    public String getDisc() {
        return disc;
    }

    public void setDisc(String disc) {
        this.disc = disc;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getPG_TYPE() {
        return PG_TYPE;
    }

    public void setPG_TYPE(String PG_TYPE) {
        this.PG_TYPE = PG_TYPE;
    }

    public String getCard_no() {
        return card_no;
    }

    public void setCard_no(String card_no) {
        this.card_no = card_no;
    }

    public String getName_on_card() {
        return name_on_card;
    }

    public void setName_on_card(String name_on_card) {
        this.name_on_card = name_on_card;
    }

    public String getUdf2() {
        return udf2;
    }

    public void setUdf2(String udf2) {
        this.udf2 = udf2;
    }

    public String getAddedon() {
        return addedon;
    }

    public void setAddedon(String addedon) {
        this.addedon = addedon;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUnmappedstatus() {
        return unmappedstatus;
    }

    public void setUnmappedstatus(String unmappedstatus) {
        this.unmappedstatus = unmappedstatus;
    }

    public String getMerchant_UTR() {
        return Merchant_UTR;
    }

    public void setMerchant_UTR(String merchant_UTR) {
        Merchant_UTR = merchant_UTR;
    }

    public String getSettled_At() {
        return Settled_At;
    }

    public void setSettled_At(String settled_At) {
        Settled_At = settled_At;
    }
}

现在工作示例如下:

public class VerifyPaymentExample {

    public static void main(String[] args) {
        String verifyPaymentDetails = "{" +
                "2298597={mihpayid=403993715508098532, request_id=NULL, bank_ref_num=NULL, amt=53.77, disc=0.00,   mode=CC, PG_TYPE=AXIS, card_no=512345XXXXXX2346, name_on_card=emu, udf2=0, addedon=2013-06-03 17:34:42, status=failure, unmappedstatus=failed, Merchant_UTR=NULL, Settled_At=NULL}," +
                "6503939={mihpayid=Not Found, status=Not Found}" +
                "}";

        Map<String, VerifyPaymentRO> result = Rojo.asMap("(\\d+)=\\{(.+?)\\}", verifyPaymentDetails)
                .entrySet()
                .stream()
                .map(e -> toBean(e))
                .collect(Collectors.toMap(VerifyPaymentRO::getId, Function.identity()));

    }

    public static VerifyPaymentRO toBean(Map.Entry<String,String> entry) {
        VerifyPaymentRO bean = new VerifyPaymentRO();
        bean.setId(entry.getKey());

        BeanMap beanMap = new BeanMap(bean);
        Rojo.asMap("(\\w.+?)=([^,]+)", entry.getValue())
            .forEach( (key, value) -> {
                //Some additional mapping is required due to non-standard naming
                if ("Settled_At".equals(key)) {
                    beanMap.put("settled_At", value);
                } else if ("Merchant_UTR".equals(key)) {
                    beanMap.put("merchant_UTR", value);
                } else {
                    beanMap.put(key, value);
                }
        });

        return bean;
    }
}

如果你运行代码并查看result,你应该得到你想要的。

于 2016-11-20T19:50:14.260 回答