package mission; //json 实体互转, list json互转 ,List<实体> 转List<Object> , List<Objcet> 转List<Map> 使用alibaba的json工具包 import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; /** * 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.setId(1); b.setName("测试"); b.setStatus(false); Brand b1 = new Brand(); b1.setId(2); b1.setName("测试2"); b1.setStatus(false); Brand b2 = new Brand(); b2.setId(3); b2.setName("测试3"); b2.setStatus(false); brandList.add(b1); brandList.add(b2); brandList.add(b); /** * 实体转为json String 字符串 */ String s = JSONObject.toJSONString(b); System.out.println(s); /** * json String 字符串 转为 JSONObject */ JSONObject ss = JSONObject.parseObject(s); System.out.println(ss.getBooleanValue("status")); /** * JSONObject 转为 String * String 转为 Map */ String sss = JSON.toJSONString(ss); Map maps = JSONObject.parseObject(sss, Map.class); for(Object object : maps.keySet()) { System.out.println("key=" + object + " value=" + maps.get(object)); } /** * * 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 转 实体 */ 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; } }
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。