1.数组转换为json字符串(用JSONArray)

                String array[] = new String[]{"one","two","three"};                
                JSONArray jsonArray= JSONArray.fromObject(array);
                System.out.println(jsonArray);

结果:["one","two","three"]


2.list转换为json字符串(用JSONArray)

                 List list = new ArrayList();            
                 list.add("1");
                 list.add("2");
                 list.add("3");
                 JSONArray jsonArray = JSONArray.fromObject(list);
                 System.out.println(jsonArray);               		

结果:["1","2","3"]


3.Map转换为json字符串(用JSONObject)

                 Map map = new HashMap<String,String>();
		 map.put("name", "junxie");
		 map.put("age", "23");
		 map.put("county", "china");
		 JSONObject jsonObject = JSONObject.fromObject(map);
		 System.out.println(jsonObject);

结果:{"county":"china","age":"23","name":"junxie"}


4.普通java对象转为json字符串

user对象:

public class User {
   public String name;
   public String age;
   public String country;
   
   public User(){}
   public User(String name,String age,String country){
	   this.name = name;
	   this.age = age;
	   this.country = country;
   }
   //省略get和set方法
 }
                 User user = new User("junxie","23","china");
		 JSONObject jsonObject = JSONObject.fromObject(user);
		 System.out.println(jsonObject);

结果:{"age":"23","country":"china","name":"junxie"}

 注意:和map的结果一样的


5.复杂的java对象转为json字符串(对象中包含list和map)

UserScores对象:

public class UserScores{
   private List scores;   //list
   private String name;
   private String age;
   private Map scores2;    //map
   
   .....//省略get和set方法
 }
		 List scores = new ArrayList();
		 scores.add("100");
		 scores.add("98");    //组装list
		 
		 Map scores2 = new HashMap<String, Integer>();
		 scores2.put("chinese", "80");
		 scores2.put("math", "90");    //组装map 
		 
		 UserScores useScores = new UserScores();
		 useScores.setAge("23");
		 useScores.setName("junxie");
		 useScores.setScores(scores); 
		 useScores.setScores2(scores2);
		 JSONObject jsonObject = JSONObject.fromObject(useScores);
		 System.out.println(jsonObject);

结果:

{"age":"23","name":"junxie","scores":["100","98"],"scores2":{"math":"90","chinese":"80"}}

注意:scores为list的生成结果,scores2为map的生成结果



6.json字符串转为普通对象

                String jsonString = "{age:'23',country:'china',name:'junxie'}"; 
		JSONObject jsonObject = JSONObject.fromObject(jsonString); 
		User user = (User)JSONObject.toBean(jsonObject,User.class);
		System.out.println(user.getName());

结果:打印的name为 junxie

注意:先生成jsonObject,再调用静态方法JSONObject.toBean(jsonObject, beanClass)



7.json字符串转为复杂对象(对象中含有list,map或者其他对象)

                String jsonList = "{age:'23',name:'junxie',scores:['100','98'],scores2:{math:'90',chinese:'80'}}";  //待解析的json字符串
		Map map = new HashMap();
		map.put("scores", String.class);  //复杂对象list
		map.put("scores2", Map.class);    //复杂对象map  (可以有其他的子对象等)
		
		JSONObject jsonObject = JSONObject.fromObject(jsonList);
		UserScores userScores= (UserScores)JSONObject.toBean(jsonObject, UserScores.class, map);     //注意该方法中要放入之前建立的map
		System.out.println(userScores);
		List<String> scores = userScores.getScores();
		System.out.println(scores);
		
		Map scores2 = userScores.getScores2();
		System.out.println(scores2);

结果:

UserScores@739495b8

[100, 98]

{math=90, chinese=80}

注意:如果要解析成复杂对象时,应该先建立Map,放入所有的复杂对象类型,最后调用方法

JSONObject.toBean(jsonObject, beanClass, classMap)  该classMap则放入所有复杂对象的map