// json 实体互转, list json互转 ,List<实体> 转List<Object> , List<Objcet> 转List<Map> 使用alibaba的json工具包 import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.dao.model.Brand; import java.util.ArrayList; import java.util.List; /** * Created by chengda on 2017/2/23. */ public class JsonTest { public static void main(String[] args) { List<Brand> brandList = new ArrayList<Brand>(); Brand b = new Brand(); b.setBrandId(1); b.setBrandName("测试"); b.setStatus(false); Brand b1 = new Brand(); b1.setBrandId(2); b1.setBrandName("测试2"); b1.setStatus(false); Brand b2 = new Brand(); b2.setBrandId(3); b2.setBrandName("测试3"); b2.setStatus(false); brandList.add(b1); brandList.add(b2); brandList.add(b); /** * *实体转为json */ String s = JSONObject.toJSONString(b); System.out.println(s); /** * *json转为实体 */ Brand brand = JSONObject.parseObject(s, Brand.class); System.out.println(brand + "=============="); /** * *list集合转json */ String s1 = JSON.toJSONString(brandList); System.out.println(s1 + "999999"); //json 转list List<Brand> brands = JSONObject.parseArray(s1, Brand.class); System.out.println(brands); } /** * List<实体>转为list<Object> * * @param t * @return */ public List<Object> beanToObject(List<?> t) { List<Object> o = new ArrayList<Object>(); Iterator<?> it = t.iterator(); while (it.hasNext()) { o.add(it.next()); } return o; } /** * List<objcet> 转为List<Map> * * @param object * @return * @throws Exception */ public List<Map> beanToMap(List<Object> object) throws Exception { List<Map> maps = new ArrayList<Map>(); for (Object o : object) { String s = JSON.toJSONString(o); Map map = JSONObject.parseObject(s, Map.class); maps.add(map); } return maps; } /** * map 转 json * 导 import net.sf.json.JSONObject; */ public String mapToJson(){ Map<String,String> map = new HashMap<String,String>(); map.put("huahua1","huahua1"); map.put("huahua2","huahua2"); map.put("huahua3","huahua3"); JSONObject smsparam = JSONObject.fromObject(smsParam); return smsparam.toJSONString(); } 另外一种map 转json 导 com.alibaba.druid.support.json;
String s = JSONUtils.toJSONString(map) /** * map 转 实体 */ public static <T> T toBean(Class<T> clazz, Map map) { T obj = null; try { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); obj = clazz.newInstance(); // 创建 JavaBean 对象 // 给 JavaBean 对象的属性赋值 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。 Object value = map.get(propertyName); if ("".equals(value)) { value = null; } Object[] args = new Object[1]; args[0] = value; try { descriptor.getWriteMethod().invoke(obj, args); } catch (InvocationTargetException e) { System.out.println("字段映射失败"); } } } } catch (IllegalAccessException e) { System.out.println("实例化 JavaBean 失败"); } catch (IntrospectionException e) { System.out.println("分析类属性失败"); } catch (IllegalArgumentException e) { System.out.println("映射错误"); } catch (InstantiationException e) { System.out.println("实例化 JavaBean 失败"); } return (T) obj; }