FastJson解析复杂字符串(string转bean)
博客专区 > BWaylon 的博客 > 博客详情
FastJson解析复杂字符串(string转bean)
BWaylon 发表于12个月前
FastJson解析复杂字符串(string转bean)
  • 发表于 12个月前
  • 阅读 150
  • 收藏 1
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

待解析字符串

{
    "BgRetUrl": "http://test1234.tunnel.qydev.com/hftgsp/notify.sp",
    "PrincipalAmt": "399.30",
    "Fee": "199.40",
    "ProId": "XM17010109AWQ",
    "SubOrdId": "950517010113253264028162508",
    "InCustId": "6000060006112192",
    "OrdDate": "20170220",
    "DivDetails": "[{\"DivCustId\":\"6000060006014887\",\"DivAcctId\":\"MDT000002\",\"DivAmt\":\"186.35\"},{\"DivCustId\":\"6000060006014887\",\"DivAcctId\":\"MDT000004\",\"DivAmt\":\"13.05\"}]",
    "MerCustId": "6000060006014887",
    "OrdId": "9510002201504707886",
    "OutCustId": "6000060006115439",
    "SubOrdDate": "20170101",
    "InterestAmt": "23.95",
    "Version": "30",
    "CmdId": "Repayment",
}

定义的bean

public class HftgRepaymentRequest {
    private String cmdId;

    private String merCustId;

    private String proId;

    private String ordId;

    private String ordDate; 

    private String subOrdId;

    private String subOrdDate; 

    private String outAcctId;
    
    private String principalAmt;

    private String interestAmt; 

    private String fee;

    private String inCustId;

    private List<HftgDivDetail> divDetails;

    private String bgRetUrl;
    
    //省略get、set
}

public class HftgDivDetail {
    @JSONField(ordinal = 1, name = "DivCustId")
    private String divCustId;

    @JSONField(ordinal = 2, name = "DivAcctId")
    private String divAcctId;
    
    @JSONField(ordinal = 3, name = "DivAmt")
    private String divAmt;
    
    //省略get、set
}

对字符串直接进行转换

HftgRepaymentRequest repayRequest = JSON.parseObject(jsonStr, HftgRepaymentRequest.class);

结果报错,错误如下:

com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 312
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:343)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:832)
	at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_HftgDivDetail.deserialze(Unknown Source)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:185)
	at com.alibaba.fastjson.parser.deserializer.ArrayListTypeFieldDeserializer.parseArray(ArrayListTypeFieldDeserializer.java:165)
	at com.alibaba.fastjson.parser.deserializer.ArrayListTypeFieldDeserializer.parseField(ArrayListTypeFieldDeserializer.java:53)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:689)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:570)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:832)
	at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_2_HftgRepaymentRequest.deserialze(Unknown Source)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:185)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:621)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:339)

查询原因发现是HftgRepaymentRequest 当中存在list对象,当bean中存在list、map、set等复杂对象时fastjson直接解析会报错,无法识别其中的“[”。

【解决方法】

既然直接解析不行,就迂回处理一下,将bean中的list对象单独解析,然后再赋值,代码如下:

            JSONObject object = JSON.parseObject(jsonStr);
            JSONArray divDetailsArray = object.getJSONArray("DivDetails");
            
            List<HftgDivDetail> divDetailList = Lists.newArrayList();
            if(null != divDetailsArray){
                divDetailList  = JSON.parseArray(divDetailsArray.toJSONString(), HftgDivDetail.class);
            }
            
            object.remove("DivDetails");
            HftgRepaymentRequest repaymentRequest = JSON.parseObject(object.toJSONString(), HftgRepaymentRequest.class);
            repaymentRequest.setDivDetails(divDetailList);

这样就可以实现想要的复杂字符串转对象,缺点是如果对象中多个类似的集合在,每个都单独解析再赋值会比较烦。

【注】项目中使用的是fastjson-1.2.21版本,没有测试最新版本的fastjson是否还存在这个问题

 

 

 

 

 

 

标签: fastjson java
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 5
码字总数 1758
×
BWaylon
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: