rt.
直接上代码:

package doc_02_json;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 模块说明: 使用java反射机制将javabean对象和对象数组分别转成json字符串
 * 
 */
public class ToJson {

    // java bean to json
    public String objToJson(Object obj) {
        StringBuilder build = new StringBuilder();
        build.append("{");
        @SuppressWarnings("rawtypes")
        Class cla = null;
        try {
            // 反射加载类
            cla = Class.forName(obj.getClass().getName());
        } catch (ClassNotFoundException e) {
            System.out.println(obj.getClass().toString().concat(" 未找到这个类"));
            e.printStackTrace();
            return null;
        }
        StringBuffer methodname = new StringBuffer();
        // 获取java类的变量
        Field[] fields = cla.getDeclaredFields();
        String separate = "";

        for (Field temp : fields) {
            build.append(separate);
            build.append("\"");
            build.append(temp.getName());
            build.append("\":");

            methodname.append("get");
            methodname.append(temp.getName().substring(0, 1).toUpperCase());
            methodname.append(temp.getName().substring(1));

            build.append("\"");
            Method method = null;
            try {
                // 获取java的get方法
                method = cla.getMethod(methodname.toString());
            } catch (NoSuchMethodException | SecurityException e) {
                methodname.setLength(0);
                e.printStackTrace();
            }

            try {
                // 执行get方法,获取变量参数的直。
                build.append(method.invoke(obj));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            build.append("\"");
            methodname.setLength(0);
            separate = ",";
        }

        build.append("}");
        return build.toString();
    }

    // array to json
    public String arrToJson(Object[] objs) {
        StringBuilder build = new StringBuilder();
        build.append("[");
        String separate = "";

        for (Object obj : objs) {
            build.append(separate);
            // object转json字符串
            String str = objToJson(obj);
            build.append(str);
            separate = ",";
        }

        build.append("]");
        return build.toString();
    }
}

Java bean:

package doc_02_json;

/**
 * 模块说明: java bean
 * 
 */
public class User {
    private int id;
    private String name;
    private int age;

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

测试:

package doc_02_json;

/**
 * 模块说明: 测试
 * 
 */
public class Test {
    public static void main(String[] args) {
        User user1 = new User(1, "zhangsan", 30);
        User user2 = new User(2, "lisi", 40);
        User user3 = new User(3, "wangwu", 25);
        User[] users = { user1, user2, user3 };
        ToJson toJson = new ToJson();
        System.out.println("将User转化为json:" + toJson.objToJson(user1));
        System.out.println("将User[]转化为json:" + toJson.arrToJson(users));
    }
}

运行结果:

java 数组转换成json java数组转json对象_java 数组转换成json

end.