2013-10-03 55 views
0

我有一個包含這個值的字符串:得到字符串值和的值複製到POJO屬性:JAVA

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} 
}" 

,我想從上面的字符串提取值,並將其存儲如下所示:

重點將是關鍵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>(); 

2298597和價值觀,這就是我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並將它們存儲爲一個HashMap中的id和object?

是否有辦法爲這個春天也蠻好

+0

您試圖將字符串反序列化爲對象。如果字符串是由您定義的,最好使用像JSON或XML這樣的通用序列化來執行這些操作並自動化該過程。無論如何,你的請求可以使用正則表達式甚至StringTokenizer來執行,但是相當長......它根本與Spring沒有關係。 – logoff

+0

@logoff你會給我一些樣品請 –

+0

如果這是一個JSON,它會一直很簡單!我認爲提取每個值並手動設置它們是唯一的方法。 – SudoRahul

回答

0

假設它不是一個JSON,你真的需要做艱苦的工作,我認爲最好的辦法是使用RojoBeanMap從公共組合-beanutils。我也是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; 
    } 
} 

如果您運行的代碼並看看結果,你應該有你想要的。