java与json,一篇就够了
本示例使用的json包为阿里的fastjson
首先写三个工具类(seter和geter方法省略,自行补上):
1 /** 2 * 屏幕实体类 3 */ 4 public class Screen { 5 6 private String resolution; // 分辨率 7 private String size; // 显存大小 8 9 }
1 /** 2 * 内存条实体类 3 */ 4 public class Memory { 5 6 private String size; // 大小 7 private String brand; // 品牌 8 9 }
1 /** 2 * 电脑实体类 3 */ 4 public class Computer { 5 6 private int id; // id 7 private String brand; // 品牌 8 private Double price; // 价格 9 private Screen screen; // 屏幕 10 private List<Memory> memory; // 内存条 11 12 }
测试类,直接上代码:
1 /** 2 * 测试类 3 */ 4 public class T01 { 5 6 public static void main(String[] args) { 7 // 1. 《自定义对象》转《json》 8 Computer c1 = getComputer(); 9 JSONObject obj = JSONObject.parseObject(JSONObject.toJSONString(c1)); 10 System.out.println(obj); 11 System.out.println("---------------------------------------------------------------------------"); 12 13 // 2. 《json》转《自定义对象》 14 String str = obj.toJSONString(); 15 Computer c2 = JSONObject.parseObject(str, Computer.class); 16 System.out.println(c2.toString()); 17 System.out.println("---------------------------------------------------------------------------"); 18 19 // 3. 《列表》转《JSONArray》 20 List<Computer> computers = getList(); 21 JSONArray ja = JSON.parseObject(JSONArray.toJSONString(computers), JSONArray.class); 22 System.out.println(ja.toJSONString()); 23 System.out.println("---------------------------------------------------------------------------"); 24 25 // 4. 《JSONArray》转《列表》 26 List<Computer> list = JSONArray.parseArray(ja.toJSONString(), Computer.class); 27 list.forEach(System.out::println); 28 System.out.println("---------------------------------------------------------------------------"); 29 30 // 5. 《数组》转《JSONArray》 31 // 用Arrays的asList方法将数组转换成List,然后按照步骤3转换就可以了 32 List<Memory> ms = Arrays.asList(new Memory[] { new Memory("8G", "金士顿"), new Memory("4G", "三星") }); 33 ms.forEach(System.out::println); 34 System.out.println("---------------------------------------------------------------------------"); 35 36 // 6. 《JSONArray》转《数组》 37 Computer[] cs = new Computer[list.size()]; 38 list.toArray(cs); // 先按照步骤4将JSONArray转成list,再转成数组 39 for (Computer computer : cs) { 40 System.out.println(computer); 41 } 42 } 43 44 private static List<Computer> getList() { 45 List<Memory> ms1 = Arrays.asList(new Memory[] { new Memory("8G", "金士顿"), new Memory("4G", "三星") }); 46 Screen s1 = new Screen("1024*768", "16G"); 47 Computer c1 = new Computer(1, "戴尔", 5678.9, s1, ms1); 48 49 List<Memory> ms2 = Arrays.asList(new Memory[] { new Memory("16G", "惠普"), new Memory("8G", "森海") }); 50 Screen s2 = new Screen("1024*768", "8G"); 51 Computer c2 = new Computer(2, "联想", 1234.5, s2, ms2); 52 53 List<Memory> ms3 = Arrays.asList(new Memory[] { new Memory("32G", "联想"), new Memory("8G", "纽曼") }); 54 Screen s3 = new Screen("1024*768", "16G"); 55 Computer c3 = new Computer(3, "苹果", 4567.8, s3, ms3); 56 57 return Arrays.asList(new Computer[] { c1, c2, c3 }); 58 } 59 60 private static Computer getComputer() { 61 List<Memory> ms = Arrays.asList(new Memory[] { new Memory("16G", "金士顿"), new Memory("8G", "三星") }); 62 return new Computer(7, "外星人", 12345.689, new Screen("1024*768", "16G"), ms); 63 } 64 65 }