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); } }}
时间: 2024-10-05 05:01:56