Gson解析json繁杂数据
碰到json数据、里面格式众多、list+string[]+等等。具体json参数如下:
eg:以下为接口参数:
"responseData":{ "brandCode": "10000002", "brandName": "健康卡", "channelId": 20001, "channelIdOldPc": "wap-fmall-yztapp", "channelIdOldWap": "wap-fmall-yztapp", "createBy": "creator", "createDate": "2016-04-26 10:30:40.000830", "ext": { "main_product_icon_url": ["http://www.google.com0", "http://www.google.com1", "http://www.google.com2"], "product_tag": "产品标签", "product_feature": "product_feature", "faq": [{ "answer": "您好!客服156,具体的支付方式需要以您的结算页面显示为准。 感谢您对京东的支持!祝您购物愉快0", "questionType": "type0", "question": "可以12期免息吗?0" }, { "answer": "您好!客服156,具体的支付方式需要以您的结算页面显示为准。 感谢您对京东的支持!祝您购物愉快4", "questionType": "type4", "question": "可以12期免息吗?4" }], "life_rule_mind": "一次一张", "life_except_date": "20150507,20150607,20150707" }, "updateDate": "2016-04-26 10:30:40.000830", "vendorCode": "10000000", "vendorName": "顺德商户", "graphic_insurance": [{ "url": "1", "name": "1" }], }
上述为接口返回的json数据。格式太多、只罗列出这些。首先一开始想到用jsonObject。后来解析着解析着各种莫名的错误。字段类型不匹配啦,日期格式错误啦等等等。搞的头大的一匹~后来百度了一把,发现了谷歌的Gson(不过需要个jar包、自己找吧。太忙了这会儿);
废话少说,上代码:
字段+格式太多了、根本不可能一个个的解析(一百七十多个、表问我为毛这么多。我也想知道为什么)。由此开启了Gson之路
//action 方法开始{ JSONObject jsonObject = EsgClientUtil.sendProductDto(ESG_URI_INFO, baseProductDto); //ESG_URI_INFO;自定义接口路径; String responseCode = jsonObject.getString("responseCode"); String responseMessage = jsonObject.getString("responseMessage"); //responseCode responseMessage 此处是返回调用接口的返回码和是否成功信息 if (jsonObject == null || !"000000".equalsIgnoreCase(responseCode)) {// 调用失败 serviceResponse.setSuccess(false); serviceResponse.setResponseMsg("调用接口失败。" + responseMessage); return serviceResponse; } serviceResponse.setSuccess(true); String reponseStr = ""; if(jsonObject.has("responseData")){ //接口是否成功返回数据 JSONObject responseData = jsonObject.getJSONObject("responseData"); reponseStr = jsonObject.getString("responseData"); //使用Gson进行json解析 (多个json解析+list数组等) //解析开始 Gson gson = new Gson(); BaseProductDto responseBaseProductDto = gson.fromJson(reponseStr, BaseProductDto.class); JSONObject extJson = responseData.getJSONObject("ext"); //对应上面eg数据里的ext; //获取ext扩展信息里的json格式数据,dto中用list<map>接收。并到页面遍历出 子选项 String ext = extJson.toString(); if(!"".equals(ext) && ext != "" && !"[]".equals(ext)){ ScalableMessageDto s = gson.fromJson(ext, ScalableMessageDto.class); //对应实体类;进行匹配解析 responseBaseProductDto.setScalableMessageDto(s); 以上就是数据的初步解析;如果里面有更多json嵌套等:继续往下解析就是了: eg(举例): String productSkuStr = responseData.getString("productSku"); //如果又来一层。接收key:productSku。然后解析就是了 if(!"".equals(productSkuStr) && productSkuStr !="" && !"[]".equals(productSkuStr)){ JSONObject productSku = JSONArray.fromString(productSkuStr).getJSONObject(0); String sku = productSku.getString("sku"); //productSku 里面的key:sku if(!"".equals(sku) && sku != "" && !"[]".equals(sku)){ ProductSkuDto productSkuDto = gson.fromJson(sku, ProductSkuDto.class); //实体类关联的productSkuDto responseBaseProductDto.setProductSkuDto(productSkuDto); } }
//实体类 public class BaseProductDto implements Serializable{ /** * 扩展信息、引入关联实体类 */ private ScalableMessageDto scalableMessageDto; /** * skuDto信息 */ private ProductSkuDto productSkuDto; //....setter/getter }
那么问题来了。方法到这差不多结束了。字段呢?繁杂且冗余的字段类型怎么定义呢。接着走~
//对应BaseProductDto 引入的实体类 public class ScalableMessageDto { private static final Log LOG = LogFactory.getLog(ScalableMessageDto.class); //公共信息模块 private List<String> publish_area; //开发文档为array格式、即允许逗号分隔多个存储等。eg:1003,1004,1008 private String deal_agreement; private String product_slogan; private String product_introduce; private String product_feature; private String product_consult; private List<Map>faq; // List<Map> 文档为json格式。eg:{"result":[{"questionType":"","qa":[{"question":"","answer":""},{"question":"","answer":""}]}, //{"questionType":"","qa":[{"question":"","answer":""},{"question":"","answer":""}]}]}。 即多个文本组合、一同存入; private String faq_url; private List<String> main_product_icon_url; private List<String> list_product_icon_url; private List<String> product_tag; private List<Map> graphic_insurance; private List<Map> insurance_respon; //。。。。setter、getter }
以上list<map>类型的优势是什么呢;只是在实体类中格式化后。页面就省事了。只需要<c:foreach>遍历即可获取所有json数据;
eg:
jsp、、faq字段为例
<tbody id="questionType" name ="questionType"> <c:forEach var="faq" items="${baseProductDto.scalableMessageDto.faq}" varStatus="faqSize"> <tr> <td><h4>常见问题<c:out value="${faqSize.index+1}"></c:out></h4></td> <td class="in w400"> <div class="qa" name="qa"><br/> <span>问题<input type="text" class="w400 ml15 js_provisionName" disabled="disabled" name="question" value="<c:out value="${faq.question}"/>"/><b class="question"></b></span><br/><br/> <span>答案<input type="text" class="w400 ml15 js_downloadUrl" disabled="disabled" name="answer" value="<c:out value="${faq.answer}"/>"/><b class="answer"></b></span><br/><br/> <span><div style="width:800px;height:1px;margin:0px auto;padding:0px;background-color:#D5D5D5;overflow:hidden;"></div> </span> </div> </td> </tr> </c:forEach> </tbody>
//ps:以上为本人工作时遇到并已解决的问题。拿出来与大家共享。[/拜谢]、
如有转发请注明出处。尊重原创~从这里开始