java的json使用需要用到6个工具包,
json-lib-2.3-jdk15.jar | Jakarta commons-lang 2.5 | Jakarta commons-beanutils 1.8.0 | Jakarta commons-collections 3.2.1 | Jakarta commons-logging 1.1.1 | ezmorph 1.0.6
其中json-lib-2.3-jdk15.jar为主包,其它5个为依赖包:
下面分别介绍,json格式与其他数据格式的转换
1. 数组与List转JSON
说明:使用JSONArray.fromObject()方法
1)数组
private static void arrayToJson() { String[] arr = {"123", "abc", "3edd", "feh"}; JSONArray jsonArr = JSONArray.fromObject(arr); System.out.println(jsonArr.toString()); } output: ["123","abc","3edd","feh"]
2)List
private static void listToJson() { List<People> peoples = new ArrayList<People>(); People p1 = new People("小明", "男"); People p2 = new People("小桃", "女"); peoples.add(p1); peoples.add(p2); JSONArray jsonArr = JSONArray.fromObject(peoples); System.out.println(jsonArr.toString()); } output: [{"name":"小明","sex":"男"},{"name":"小桃","sex":"女"}]
People类
public class People { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public People(String name, String sex) { this.name = name; this.sex = sex; } }
附:注意要重写变量的set和get方法。
2. 对象和map转json
说明:使用JSONObject.fromObject()方法
1)对象
private static void objectToJson() { People p1 = new People("小明", "男"); JSONObject jsonObj = JSONObject.fromObject(p1); System.out.println(jsonObj.toString()); } output: {"name":"小明","sex":"男"}
2)Map
private static void mapToJson() { Map<String, String> map = new HashMap<String, String>(); map.put("随遇而安", "孟非的人生经历"); map.put("浪潮之巅", "讲诉互联网公司的兴衰与变革"); JSONObject jsonObj = JSONObject.fromObject(map); System.out.println(jsonObj.toString()); } output: {"随遇而安":"孟非的人生经历","浪潮之巅":"讲诉互联网公司的兴衰与变革"
3. 将json字符串转换为对象
说明:使用JSONObject.toBean()方法
private static void jsonToObj() { String jsonStr = "{\"name\":\"小明\",\"sex\":\"男\"}"; JSONObject jsonObj = JSONObject.fromObject(jsonStr); People p = (People)JSONObject.toBean(jsonObj, People.class); System.out.println("name: "+p.getName()+"/sex: "+p.getSex()); } output: name: 小明/sex: 男
附:运行时我这边出现:java.lang.NoSuchMethodException: json.People.<init>()错误
原因是没有People()构造方法,即必须加一个空的构造方法
4. json字符串的解析
虽然json字符串直接可以映射到具体的对象,但也可以直接解析出json格式,再封装成一个对象
在People加上一个变量:friends, 类型为List<People>,为其增加set,get方法
private static void parseJsonStr() { People p1 = new People("小明", "男"); List<People> friends = new ArrayList<People>(); People f1 = new People("小朱", "男"); People f2 = new People("小桃", "女"); friends.add(f1); friends.add(f2); p1.setFriends(friends); JSONObject jsonObj = JSONObject.fromObject(p1); System.out.println(jsonObj.toString()); String jsonStr = jsonObj.toString(); JSONObject obj = JSONObject.fromObject(jsonStr); People p = (People)JSONObject.toBean(obj, People.class); System.out.println("name: "+p.getName()+"/sex: "+p.getSex()+"/friends: "+p.getFriends()); }
说明:上面的一种方式是直接通过toBean方法,转换为people对象,
当然还可以直接解析,如下:
private static void parseJsonStr() { String jsonStr = "{\"friends\":[{\"friends\":[],\"name\":\"小朱\",\"sex\":\"男\"},{\"friends\":[],\"name\":\"小桃\",\"sex\":\"女\"}],\"name\":\"小明\",\"sex\":\"男\"}"; JSONObject obj = JSONObject.fromObject(jsonStr); People p = new People(); p.setName(obj.getString("name")); p.setSex(obj.getString("sex")); JSONArray jsonArr = obj.getJSONArray("friends"); List<People> friends = new ArrayList<People>(); for(int i=0;i<jsonArr.size();i++) { People f = new People(); f.setName(jsonArr.getJSONObject(i).getString("name")); f.setSex(jsonArr.getJSONObject(i).getString("sex")); friends.add(f); } p.setFriends(friends); System.out.println("name: "+p.getName()+"/sex: "+p.getSex()+"/friends: "+JSONArray.fromObject(p.getFriends()).toString()); }