json转换学习
文章部分代码实体类可以忽略。
原文:https://www.cnblogs.com/free-dom/p/5801866.html
代码如下:
1 package covert; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import net.sf.json.JSONArray; 9 import net.sf.json.JSONObject; 10 11 /** 12 * 13 * @author jack 陈 14 * 15 */ 16 public class Student { 17 18 private String name; 19 private String address; 20 private String age; 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public String getAddress() { 31 return address; 32 } 33 34 public void setAddress(String address) { 35 this.address = address; 36 } 37 38 public String getAge() { 39 return age; 40 } 41 42 public void setAge(String age) { 43 this.age = age; 44 } 45 46 public Student(String name, String address, String age) { 47 super(); 48 this.name = name; 49 this.address = address; 50 this.age = age; 51 } 52 53 public Student() { 54 super(); 55 56 } 57 58 @Override 59 public String toString() { 60 return "Student [name=" + name + ", address=" + address + ", age=" + age + ", getName()=" 61 + getName() + ", getAddress()=" + getAddress() + ", getAge()=" + getAge() 62 + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" 63 + super.toString() + "]"; 64 } 65
66 /** 67 * 68 * json转化,数组,对象 69 */ 70 public static void convertjson() { 71 72 Student s = new Student(); 73 s.setAddress("北京东城区"); 74 s.setAge("56"); 75 s.setName("刘德华"); 76 77 // 对象 78 JSONObject jsonObject = JSONObject.fromObject(s); 79 // 数组 80 JSONArray jsonArray = JSONArray.fromObject(s); 81 82 // 转化 83 String js1 = jsonObject.toString(); 84 String js2 = jsonArray.toString(); 85 86 // 打印 87 System.out.println(js1); 88 System.out.println(js2); 89 90 } 91
92 //json 转 字符串 93 public static void jsontostring() { 94 // 定义两种不同格式的字符串 95 String objectStr = "{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}"; 96 String arrayStr = "[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}]"; 97 98 // 1、使用JSONObject 99 JSONObject object = JSONObject.fromObject(objectStr); 100 Student stu = (Student) JSONObject.toBean(object, Student.class); 101 102 // 2、使用JSONArray 103 JSONArray jsonArray = JSONArray.fromObject(arrayStr); 104 // 获得jsonArray的第一个元素 105 Object o = jsonArray.get(0); 106 JSONObject jsonObject2 = JSONObject.fromObject(o); 107 Student stu2 = (Student) JSONObject.toBean(jsonObject2, Student.class); 108 System.out.println("stu:" + stu); 109 System.out.println("stu2:" + stu2); 110 111 } 112
113 // list转json(注意:list只能转json数组) 114 public static void listtojson() { 115 Student stu = new Student(); 116 stu.setName("JSON"); 117 stu.setAge("23"); 118 stu.setAddress("北京市海淀区"); 119 List<Student> list = new ArrayList<Student>(); 120 list.add(stu); 121 122 // 转化 123 //JSONObject listobjest = JSONObject.fromObject(list); 124 JSONArray listarray = JSONArray.fromObject(list); 125 126 //打印 127 //System.out.println(listobjest.toString()); 128 System.out.println(listarray.toString()); 129 } 130
131 // json转list 132 public static void jsontolist() { 133 134 String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}]"; 135 //转化为list 136 List<Student> list2= JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class); 137 for (Student stu : list2) { 138 System.out.println(stu); 139 } 140 //转化为数组 141 142 Student[] array = (Student[]) JSONArray.toArray(JSONArray.fromObject(arrayStr), Student.class); 143 for (Student stu2 : array) { 144 System.out.println(stu2); 145 } 146 147 } 148
149 //map转json 150 public static void mapToJSON(){ 151 Student stu=new Student(); 152 stu.setName("JSON"); 153 stu.setAge("23"); 154 stu.setAddress("中国上海"); 155 Map<String,Student> map=new HashMap<String,Student>(); 156 map.put("first", stu); 157 158 //1、JSONObject 159 JSONObject mapObject=JSONObject.fromObject(map); 160 System.out.println("mapObject"+mapObject.toString()); 161 162 //2、JSONArray 163 JSONArray mapArray=JSONArray.fromObject(map); 164 System.out.println("mapArray:"+mapArray.toString()); 165 } 166
167 //json转map 168 public static void jsonToMap(){ 169 String strObject="{\"first\":{\"address\":\"中国上海\",\"age\":\"23\",\"name\":\"JSON\"}}"; 170 171 //JSONObject 172 JSONObject jsonObject=JSONObject.fromObject(strObject); 173 Map map=new HashMap(); 174 map.put("first", Student.class); 175 176 //使用了toBean方法,需要三个参数 177 MyBean my=(MyBean)JSONObject.toBean(jsonObject, MyBean.class, map); 178 System.out.println(my.getFirst()); 179 180 } 181
182 183 public static void main(String[] args) { 184 //convertjson(); 185 System.out.println("==================="); 186 //jsontostring(); 187 System.out.println("==================="); 188 //listtojson(); 189 System.out.println("==================="); 190 //jsontolist(); 191 System.out.println("==================="); 192 mapToJSON(); 193 System.out.println("==================="); 194 jsonToMap(); 195 } 196 197 198 }
MyBean类:
1 package covert; 2 /** 3 * 4 * @author 5 * 6 */ 7 public class MyBean { 8 9 private Student first; 10 11 public Student getFirst() { 12 return first; 13 } 14 15 public void setFirst(Student first) { 16 this.first = first; 17 } 18 19 }
显示结果:
本文来自博客园,作者:土木转行的人才,转载请注明原文链接