zoukankan      html  css  js  c++  java
  • 【Java】提取JSON数值时遇到数组集合时使用的K-V方式转换

    1.实体类转换方法

    参照文章:http://www.cnblogs.com/dflmg/p/6933811.html

    2.K-V方法(此方法比较笨,但是没有办法,我现在不知道有没有相关的简单API,只能自己手动拼出一个方法。import org.apache.commons.lang.StringUtils;)

    String s = "{'request': {'enterpriseid': 55,'ownerCode': 'SunEee01','item': [{'itemName': '1熊孩子新疆无花果成品268g','itemType': 'ZC','barCode': '6924459400256','shelfLife ': 0, 'itemCode': 'xhzwhggcp_268'},
    {'itemName': '2好孩子新疆无花果成品268g','itemType': 'ZC','barCode': '6924459400256','shelfLife ': 1, 'itemCode': 'xhzwhggcp_268'}]}}"; //String s = "{'request': {'enterpriseid': 55,'ownerCode': 'SunEee01','item': [{'itemName': '好孩子新疆无花果成品268g','itemType': 'ZC','barCode': '6924459400256','shelfLife ': 1, 'itemCode': 'xhzwhggcp_268'}]}}"; JSONObject jsonObject = JSONObject.fromObject(s); //System.out.println(jsonObject); String request = jsonObject.getString("request"); //System.out.println(request); JSONObject vendorDO = JSONObject.fromObject(request); //System.out.println(vendorDO); String ent = vendorDO.getString("item"); //对象实体类的方法System.out.println(ent); /*JSONArray ents = vendorDO.getJSONArray("item"); List<Item> ss = (List<Item>)JSONArray.toList(ents,Item.class); for(Item x:ss){
    //如果item里面还有List对象作为属性就使用1的方法,参见http://www.cnblogs.com/dflmg/p/6933811.html System.out.println(x.getItemName()); }
    */ String[] many = ent.split("\},\{"); if(many.length==1){ String one = many[0].substring(1,ent.length()-1); JSONObject joss = JSONObject.fromObject(one); String itemName = joss.getString("itemName"); System.out.println(itemName); }else{ for(int i=0;i<many.length;i++){ if(i==0){ String frist = many[i].substring(1)+"}"; JSONObject joss = JSONObject.fromObject(frist); String itemName = joss.getString("itemName"); System.out.println(itemName); }else if(i==many.length-1){ String middle = "{"+StringUtils.substringBefore(many[i], "]"); JSONObject joss = JSONObject.fromObject(middle); String itemName = joss.getString("itemName"); System.out.println(itemName); }else{ String last = "{"+many[i]+"}"; JSONObject joss = JSONObject.fromObject(last); String itemName = joss.getString("itemName"); System.out.println(itemName); } }}
  • 相关阅读:
    vue从详情页回到列表页,停留在之前的tab上
    vue-touch监听手指左滑右滑事件
    vue事件代理
    vue通过ref获取组件渲染后的dom(this.$refs.xxxRef.$el)
    vue水印-第二种方法:通过指令
    # 有时候代码超时
    # 今天的leetcode1268又用上了二分搜索。
    # linux命令小常识
    # 大家好
    今天尝试配置maven的时候
  • 原文地址:https://www.cnblogs.com/dflmg/p/7182976.html
Copyright © 2011-2022 走看看