一. 将数组转换为JSON:
String[] arr = {"asd","dfgd","asd","234"};
JSONArray jsonarray = JSONArray.fromObject(arr);
System.out.println(jsonarray);
二.对象转换成JSON:
UserInfo user = new UserInfo(1001,"张三");
JSONArray jsonArray = JSONArray.fromObject(user);
System.out.println( jsonArray );
三.把Map转换成json, 要使用jsonObject对象:
Map map = new HashMap();
map.put("userId", 1001);
map.put("userName", "张三");
map.put("userSex", "男");
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println(jsonObject);
四.把List转换成JSON数据:
List list = new ArrayList();
UserInfo user = new UserInfo(1001, "张三");
list.add(user);
list.add(user);
list.add(user);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);