json对象互转

public static void main(String[] args) {
    List brandList = new ArrayList();
    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 brands = JSONObject.parseArray(s1, Brand.class);
    System.out.println(brands);


}


/**
 * List<实体>转为list
 *
 * @param t
 * @return
 */
public List beanToObject(List t) {
    List o = new ArrayList();
    Iterator it = t.iterator();
    while (it.hasNext()) {
        o.add(it.next());
    }

    return o;
}


/**
 * List 转为List
 *
 * @param object
 * @return
 * @throws Exception
 */
public List beanToMap(List object) throws Exception {
    List maps = new ArrayList();
    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 map = new HashMap();
    map.put("huahua1", "huahua1");
    map.put("huahua2", "huahua2");
    map.put("huahua3", "huahua3");
    JSONObject smsparam = JSONObject.fromObject(smsParam);
    return smsparam.toJSONString();

}

/**
 * map 转 实体
 */
public static  T toBean(Class 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;
}

你可能感兴趣的:(工具类,json,java,开发语言)