NutzCN Logo
精华 nutz解析json
发布于 3076天前 作者 qqfe28902f 3952 次浏览 复制 上一个帖子 下一个帖子
标签:
  1. 对应json是这样的:
{"alipay_trade_refund_response":{"code":"10000","msg":"Success","buyer_logon_id":"250***@qq.com","buyer_user_id":"2088312601320183","fund_change":"Y","gmt_refund_pay":"2016-07-21 15:32:39","open_id":"20881009092171344200077291812718","out_trade_no":"201605051417158819","refund_detail_item_list":[{"amount":"0.01","fund_channel":"ALIPAYACCOUNT"}],"refund_fee":"0.01","send_back_fee":"0.01","trade_no":"2016050521001004180235368850"},"sign":"SNg6pyF3QysWMBRQnIxJ3jdPch6ve17f01MSc3SWsrMgpCkYWiAabS81U3HjIWg5UIuG1bjpWk/oC8rZ2uUs0+2x9cOIycg5Cmlbg/B70oL4fsENeJm5HuMI/xe4RpX2osHG75HUdOlfKN+ZKoZfGwZ5UsPPcw5gLQ133uK+DN4="}

对应实体结构大致咋建立,才能取到code等属性
之前用Json.fromJsonAsMap方法 建立的实体
报错:

BorningException:Fail to born 'com.wx.bussiness.po.RefundRespond'
 by args: [
  @(SNg6pyF3QysWMBRQnIxJ3jdPch6ve17f01MSc3SWsrMgpCkYWiAabS81U3HjIWg5UIuG1bjpWk/oC8rZ2uUs0+2x9cOIycg5Cmlbg/B70oL4fsENeJm5HuMI/xe4RpX2osHG75HUdOlfKN+ZKoZfGwZ5UsPPcw5gLQ133uK+DN4=)]
	at org.nutz.castor.Castors.cast(Castors.java:264)
	at org.nutz.castor.Castors.castTo(Castors.java:318)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.injectMap(ObjConvertImpl.java:133)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.inject(ObjConvertImpl.java:77)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.convert(ObjConvertImpl.java:65)
	at org.nutz.mapl.Mapl.maplistToObj(Mapl.java:34)
	at org.nutz.json.Json.parse(Json.java:77)
	at org.nutz.json.Json.fromJson(Json.java:71)
	at org.nutz.json.Json.fromJson(Json.java:93)
	at org.nutz.json.Json.fromJsonAsMap(Json.java:392)
	at com.gd.cmp.test.MessageTest.main(MessageTest.java:53)

菜鸟一枚,求指导!

3 回复

格式化一下json才清晰 https://jsonformatter.curiousconcept.com/

{
   "alipay_trade_refund_response":{
      "code":"10000",
      "msg":"Success",
      "buyer_logon_id":"250***@qq.com",
      "buyer_user_id":"2088312601320183",
      "fund_change":"Y",
      "gmt_refund_pay":"2016-07-21 15:32:39",
      "open_id":"20881009092171344200077291812718",
      "out_trade_no":"201605051417158819",
      "refund_detail_item_list":[
         {
            "amount":"0.01",
            "fund_channel":"ALIPAYACCOUNT"
         }
      ],
      "refund_fee":"0.01",
      "send_back_fee":"0.01",
      "trade_no":"2016050521001004180235368850"
   },
   "sign":"SNg6pyF3QysWMBRQnIxJ3jdPch6ve17f01MSc3SWsrMgpCkYWiAabS81U3HjIWg5UIuG1bjpWk/oC8rZ2uUs0+2x9cOIycg5Cmlbg/B70oL4fsENeJm5HuMI/xe4RpX2osHG75HUdOlfKN+ZKoZfGwZ5UsPPcw5gLQ133uK+DN4="
}

从最外层开始, 是一个Map,所以建一个类

public class XXXXRespOut {
     @JsonField("alipay_trade_refund_response")
     private AlipayResp resp;
	 private String sign;
}

然后里面一层,再建一个类

public class AlipayResp {
       String code;
	   // .... 其他属性
}

转换代码

Json.fromJson(XXXXRespOut.class, str);

另外一个办法,转为NutMap,然后直接取, 或通过EL取.

NutMap map = Json.fromJson(NutMap.class, str);
Map alipay_trade_refund_response = map.getAs(Map.class, "alipay_trade_refund_response");
String code = (String)alipay_trade_refund_response.get("code");

@wendal 解决了
第一种方法可以了。
第二个方法。木有The method getAs(String, Class) in the type NutMap is not applicable for the arguments (Class, String);估计版本太低了nutz-1.r.55

那我写错了,应该是

map.getAs("alipay_trade_refund_response", Map.class);

添加回复
请先登陆
回到顶部