json 實體互轉, list json互轉 ,List 轉List , List 轉List map 轉實體 , map轉json
阿新 • • 發佈:2019-02-14
// 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; } }