JSON学习,代码总结(一)
代码经验总结:
1.json只是一种文本格式,json格式的文本(比如json格式的字符串)与json对象不是同一个东西,虽然打印效果相同,但json对象却被JSON赋予了特性
2.对json格式的文本是没法将其反序列化为指定对象,然后进一步对对象属性操作的,这大概就是json格式文本与json对象的区别
3.json对象没有自己独特的方法,它只是一个从字符串到指定对象的中转站
实体类:
1 package com.json.test; 2 3 public class Student { 4 private String code; 5 private String name; 6 private int age; 7 8 public Student() { 9 super(); 10 } 11 12 public Student(String code, String name, int age) { 13 super(); 14 this.code = code; 15 this.name = name; 16 this.age = age; 17 } 18 19 public String getCode() { 20 return code; 21 } 22 23 public void setCode(String code) { 24 this.code = code; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public int getAge() { 36 return age; 37 } 38 39 public void setAge(int age) { 40 this.age = age; 41 } 42 43 }
测试方法:
1 package com.json.test; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Set; 9 10 import com.alibaba.fastjson.JSON; 11 import com.alibaba.fastjson.JSONArray; 12 13 public class TestJson { 14 15 public static void main(String[] args) { 16 methodOne(); 17 // methodTwo(); 18 // methodThree(); 19 // methodFour(); 20 // mehtodFive(); 21 // methodSix(); 22 // methodServen(); 23 // methodEight(); 24 } 25 26 // 把JSON格式的文本parse为JSONObject或者JSONArray 27 static void methodOne() { 28 // 这是一个json格式的字符串对象 29 String str = "{'code':'1367002069', 'name':'pengpeng', 'age':24}"; 30 31 // 将json格式的字符串序列化为json对象 32 Object obj = JSON.parse(str); 33 34 // 打印的结果为:{"code":"1367002069","name":"pengpeng","age":24}.但只是一种表现,它还有JSON类赋予它的特性。 35 System.out.println(obj); 36 37 } 38 39 // 把JSON文本parse成JSONArray 40 static void methodTwo() { 41 // 这是一个json格式的字符串对象 42 String str = "[{'code':'1367002069', 'name':'pengpeng', 'age':24},{'code':'1367002069', 'name':'pengpeng', 'age':24}]"; 43 44 JSONArray jsonArray = JSON.parseArray(str); 45 System.out.println("jsonArray: " + jsonArray); 46 47 // 无法将其parse为JSONObject,猜测:json对象只能是对应于一个{}内的对象。 48 // Object obj = JSON.parseObject(str); 49 50 } 51 52 // 把JSON文本parse成JSONObject 53 static void methodThree() { 54 String strOne = "{'code':'1367002069', 'name':'pengpeng', 'age':24}"; 55 String strTwo = "[{'code':'1367002069', 'name':'pengpeng', 'age':24},{'code':'1367002069', 'name':'pengpeng', 'age':24}]"; 56 57 // 打印结果:objOne: {"code":"1367002069","name":"pengpeng","age":24} 58 Object objOne = JSON.parseObject(strOne); 59 System.out.println("objOne: " + objOne); 60 } 61 62 // 把JSON文本parse为JSONObject或者JSONArray,虽然JSON.parse(str)方法返回类型只有Object 63 static void methodFour() { 64 String str = "[{'code':'1367002069', 'name':'pengpeng', 'age':24},{'code':'1367002069', 'name':'pengpeng', 'age':24}]"; 65 Object obj = JSON.parse(str); 66 System.out.println("obj: " + obj); 67 68 } 69 70 // 把JSON文本parse成JavaBean与JavaBean集合 71 static void mehtodFive() { 72 String strOne = "{'code':'1367002069', 'name':'pengpeng', 'age':24}"; 73 String strTwo = "[{'code':'1367002069', 'name':'pengpeng', 'age':24},{'code':'1367002069', 'name':'pengpeng', 'age':24}]"; 74 75 // 也可以指定反序列化类型,打印结果:指定类型specifyObj: com.json.test.Student@2077d4de 76 Student objOne = JSON.parseObject(strOne, Student.class); 77 System.out.println("指定类型objOne: " + objOne); 78 79 // 打印结果:jsonArray:[com.json.test.Student@5387f9e0, 80 // com.json.test.Student@6e5e91e4] 81 List<Student> objArray = JSON.parseArray(strTwo, Student.class); 82 System.out.println("jsonArray:" + objArray); 83 84 } 85 86 // 将JavaBean序列化为JSON文本 87 static void methodSix() { 88 89 // List集合 90 List<Student> list = new ArrayList<Student>(); 91 Student studentOne = new Student("1367002069", "pengpeng", 24); 92 Student studentTwo = new Student("1367002070", "xiaoming", 25); 93 list.add(studentOne); 94 list.add(studentTwo); 95 96 // Map集合 97 Map<String, Student> map = new HashMap<String, Student>(); 98 map.put("stuOne", studentOne); 99 map.put("stuTwo", studentTwo); 100 101 // Set集合 102 Set<Student> set = new HashSet<Student>(); 103 set.add(studentOne); 104 set.add(studentTwo); 105 106 // 打印结果:str: {"age":24,"code":"1367002069","name":"pengpeng"} 107 String str = JSON.toJSONString(studentOne); 108 System.out.println("str: " + str); 109 110 // 打印结果:strlist: 111 // [{"age":24,"code":"1367002069","name":"pengpeng"},{"age":25,"code":"1367002070","name":"xiaoming"}] 112 String strlist = JSON.toJSONString(list); 113 System.out.println("strlist: " + strlist); 114 115 // 打印结果:strMap: 116 // {"stuOne":{"age":24,"code":"1367002069","name":"pengpeng"},"stuTwo":{"age":25,"code":"1367002070","name":"xiaoming"}} 117 String strMap = JSON.toJSONString(map); 118 System.out.println("strMap: " + strMap); 119 120 // 打印结果:strSet: 121 // [{"age":24,"code":"1367002069","name":"pengpeng"},{"age":25,"code":"1367002070","name":"xiaoming"}] 122 String strSet = JSON.toJSONString(set); 123 System.out.println("strSet: " + strSet); 124 125 } 126 127 // 将JavaBean序列化为带格式的JSON文本 128 static void methodServen() { 129 Student student1 = new Student("1367002069", "pengpeng", 24); 130 131 String jsonString = JSON.toJSONString(student1, true); 132 System.out.println("jsonString:" + jsonString); 133 134 } 135 136 // 将JavaBean转换为JSONObject或者JSONArray 137 static void methodEight() { 138 Student student1 = new Student("1367002069", "pengpeng", 24); 139 140 Object jsonObject = JSON.toJSON(student1); 141 System.out.println("jsonObject: " + jsonObject); 142 143 } 144 145 }

浙公网安备 33010602011771号