json--各种json格式转换
一:String转List<class<?>>实体类集合的方法1
1 import net.sf.json.JSONArray; 2 import net.sf.json.JsonConfig; 3 4 String json="[{\"cargoPurchasingPriceStr\":\"\",\"cargoLink\":\"\",\"cargoValueStr\":\"2000.00\",\"cargoValue\":2000.0,\"gmtCreated\":1483200000000,\"cargoTypeNameEn\":\"mobile phone\"},{\"cargoPurchasingPriceStr\":\"\",\"cargoLink\":\"\",\"cargoValueStr\":\"2000.00\",\"cargoValue\":2000.0,\"gmtCreated\":1483200000000,\"cargoTypeNameEn\":\"mobile phone\"}]"; 5 6 //第一个参数JSONArray.fromObject(json),第二个参数为实体类,第三个参数为new JsonConfig()。 7 List<Cargo> list=JSONArray.toList(JSONArray.fromObject(json), new Cargo(), new JsonConfig()); 8 9 System.out.println(list.get(0).getCargoValueStr());//2000.00
二:String转List<class<?>>实体类集合的方法2
1 import net.sf.json.JSONArray; 2 3 String stjson = "[{\\\"cargoPurchasingPriceStr\\\":\\\"\\\",\\\"cargoLink\\\":\\\"\\\",\\\"cargoValueStr\\\":\\\"2000.00\\\",\\\"cargoValue\\\":2000.0,\\\"gmtCreated\\\":1483200000000,\\\"cargoTypeNameEn\\\":\\\"mobile phone\\\",\\\"cargoTotalPriceStr\\\":\\\"0.00\\\",\\\"cargoDescription\\\":\\\"\\\",\\\"cargoTotalPrice\\\":0.0,\\\"volumeStr\\\":\\\"\\\",\\\"createdTime\\\":1483200000000,\\\"gmtModifiedStr\\\":\\\"\\\",\\\"ecommerceNo\\\":\\\"SHIP\\\",\\\"weight\\\":200.0,\\\"cargoName\\\":\\\"小米手机\\\",\\\"cargonameEn\\\":\\\"miphone\\\",\\\"cargoPriceStr\\\":\\\"\\\",\\\"cargoSpec\\\":\\\"\\\",\\\"cargoSerial\\\":\\\"\\\",\\\"reserved9Str\\\":\\\"\\\",\\\"cargoTypeName\\\":\\\"手机\\\",\\\"isDeleted\\\":\\\"0\\\",\\\"cargoCurrency\\\":\\\"RMB\\\",\\\"weightStr\\\":\\\"200.00\\\",\\\"quantity\\\":5,\\\"createdTimeStr\\\":\\\"2017-01-01\\\",\\\"gmtCreatedStr\\\":\\\"2017-01-01\\\",\\\"cargoTotalPruchasingPriceStr\\\":\\\"\\\",\\\"cargoOriginName\\\":\\\"CN\\\",\\\"cargoMeasureUnit\\\":\\\"\\\"},{\\\"cargoPurchasingPriceStr\\\":\\\"\\\",\\\"cargoLink\\\":\\\"\\\",\\\"cargoValueStr\\\":\\\"2000.00\\\",\\\"cargoValue\\\":2000.0,\\\"gmtCreated\\\":1483200000000,\\\"cargoTypeNameEn\\\":\\\"mobile phone\\\",\\\"cargoTotalPriceStr\\\":\\\"0.00\\\",\\\"cargoDescription\\\":\\\"\\\",\\\"cargoTotalPrice\\\":0.0,\\\"volumeStr\\\":\\\"\\\",\\\"createdTime\\\":1483200000000,\\\"gmtModifiedStr\\\":\\\"\\\",\\\"ecommerceNo\\\":\\\"SHIP\\\",\\\"weight\\\":200.0,\\\"cargoName\\\":\\\"魅族手机\\\",\\\"cargonameEn\\\":\\\"miphone\\\",\\\"cargoPriceStr\\\":\\\"\\\",\\\"cargoSpec\\\":\\\"\\\",\\\"cargoSerial\\\":\\\"\\\",\\\"reserved9Str\\\":\\\"\\\",\\\"cargoTypeName\\\":\\\"手机\\\",\\\"isDeleted\\\":\\\"0\\\",\\\"cargoCurrency\\\":\\\"RMB\\\",\\\"weightStr\\\":\\\"200.00\\\",\\\"quantity\\\":5,\\\"createdTimeStr\\\":\\\"2017-01-01\\\",\\\"gmtCreatedStr\\\":\\\"2017-01-01\\\",\\\"cargoTotalPruchasingPriceStr\\\":\\\"\\\",\\\"cargoOriginName\\\":\\\"CN\\\",\\\"cargoMeasureUnit\\\":\\\"\\\"}]"; 4 //replaceAll里面用的是正则表达式,所以字符串转义一次,正则转义一次,所以一个斜扛要写4个. 5 String json=stjson.replaceAll("\\\\", ""); 6 JSONArray json = JSONArray.fromObject(json); 7 List<Class<T>> users = (List<Class<T>>) JSONArray.toCollection(json, 8 Class.class); 9 System.out.println(users.get(0).getFields());
三:String转Class<T>实体类的方法
1 import net.sf.json.JSONObject; 2 3 public static <T> void main(String[] args) { 4 String stjson = "{\\\"cargoPurchasingPriceStr\\\":\\\"\\\",\\\"cargoLink\\\":\\\"\\\",\\\"cargoValueStr\\\":\\\"2000.00\\\",\\\"cargoValue\\\":2000.0,\\\"gmtCreated\\\":1483200000000,\\\"cargoTypeNameEn\\\":\\\"mobile phone\\\",\\\"cargoTotalPriceStr\\\":\\\"0.00\\\",\\\"cargoDescription\\\":\\\"\\\",\\\"cargoTotalPrice\\\":0.0,\\\"volumeStr\\\":\\\"\\\",\\\"createdTime\\\":1483200000000,\\\"gmtModifiedStr\\\":\\\"\\\",\\\"ecommerceNo\\\":\\\"SHIP\\\",\\\"weight\\\":200.0,\\\"cargoName\\\":\\\"魅族手机\\\",\\\"cargonameEn\\\":\\\"miphone\\\",\\\"cargoPriceStr\\\":\\\"\\\",\\\"cargoSpec\\\":\\\"\\\",\\\"cargoSerial\\\":\\\"\\\",\\\"reserved9Str\\\":\\\"\\\",\\\"cargoTypeName\\\":\\\"手机\\\",\\\"isDeleted\\\":\\\"0\\\",\\\"cargoCurrency\\\":\\\"RMB\\\",\\\"weightStr\\\":\\\"200.00\\\",\\\"quantity\\\":5,\\\"createdTimeStr\\\":\\\"2017-01-01\\\",\\\"gmtCreatedStr\\\":\\\"2017-01-01\\\",\\\"cargoTotalPruchasingPriceStr\\\":\\\"\\\",\\\"cargoOriginName\\\":\\\"CN\\\",\\\"cargoMeasureUnit\\\":\\\"\\\"}"; 5 //replaceAll里面用的是正则表达式,所以字符串转义一次,正则转义一次,所以一个斜扛要写4个. 6 String jeson=stjson.replaceAll("\\\\", ""); 7 JSONObject jsonobject = JSONObject.fromObject(jeson); 8 Class<T> user= (Class<T>)JSONObject.toBean(jsonobject,Class.class); 9 System.out.println(user.getFields()); 10 }
注:遇到各种json转换格式的信息并且本人实验通过后会添加

浙公网安备 33010602011771号