日期:2014-05-16  浏览次数:20383 次

简单java转json
package org.msf.json;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *@author masf
 *@date Jan 15, 2010
 */
public class JSONHelp {
        
        public static void main(String[] args) throws Exception{
                List list = new ArrayList();
                list.add("fdsfd");
                list.add(3234);
                /*String aa[] = {"11","22",null,"33"};
                aa = (String[]) Array.newInstance(String.class, 3);
                
                for(String a:aa){
                         System.out.println(a+Array.getLength(aa));
                         Array.getLength(aa);
                         
                }*/
                Student student = new Student("号楼",23,new HashMap(),list,new Student("",33,new HashMap(),list,null));

                System.out.println(
                                new JSONHelp().getJson(student)
                );
                
                //for(Field field : student.getClass().getDeclaredFields()){
                        //System.out.println(Introspector.decapitalize("getname"));
                //}
                
        }
        /**
         * 目前方法只支持基本数据类型 不包括Map Set List等
         * @param obj
         * @return
         * @throws IntrospectionException
         * @throws IllegalArgumentException
         * @throws IllegalAccessException
         * @throws InvocationTargetException
         * @author masf
         */
        public String getJson(Object obj) throws Exception{
                if(obj == null) 
                        throw new RuntimeException("javabean实例为空");
                
                BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
                //获得bean的属性集合 
                PropertyDescriptor[] beanPropertys = beanInfo.getPropertyDescriptors();
                StringBuffer str = new StringBuffer();
                str.append("{");
                for(PropertyDescriptor beanProperty : beanPropertys){
                        Method method = beanProperty.getReadMethod();
                        if(beanProperty.getName().equals("class")) 
                                continue;
                        
                        Object returnType = method.invoke(obj);
                        str.append("'"+beanProperty.getName()+"'");
                        //数字不加单引号
                        if(Number.class.isInstance(returnType)){
                                str.append(":").append(returnType).append(",");
                                continue;
                        }
                                        
                        str.append(":'").append(returnType).append("',");
                }
                str.deleteCharAt(str.length()-1).append("}");
                return str.toString();
        }
        
}


/////////////////////////////////////////////////////////////////////////////////
package org.msf.json;

import java.util.List;
import java.util.Map;

/**
 *@author masf
 *@date Jan 15, 2010
 */
public class Student {
        private String name;
        private int sex;
        @SuppressWarnings("unchecked")
        private Map map;
        private Student student;
        @SuppressWarnings("unchecked")
        private List list;
        
        @SuppressWarnings("unchecked")
        public Student(String name , int sex,Map map,List list,Student student){
                this.name = name;
                this.sex = sex;
                this.map = map;
                this.list = list;
                this.student = student;
        }
        
        public String getName() {
                return name;
        }