解析json之net.sf.json
下载地址
本次使用版本:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/
最新版本:http://sourceforge.net/projects/json-lib/files/json-lib/
使用net.sf.json需要导入的包
JSONObject
1 package com.itlwc.test; 2 3 import net.sf.json.JSONArray; 4 import net.sf.json.JSONObject; 5 6 public class Test { 7 public static void main(String[] args) { 8 // 创建JSONObject 9 JSONObject jsonObject = new JSONObject(); 10 jsonObject.put("username", "lwc"); 11 jsonObject.put("password", "123"); 12 // 打印:1 13 System.out.println(jsonObject); 14 15 // 增加属性,打印:2 16 jsonObject.element("sex", "男"); 17 System.out.println(jsonObject); 18 19 // 根据key返回,打印:3 20 System.out.println(jsonObject.get("sex")); 21 22 // 判读输出对象的类型 23 boolean isArray = jsonObject.isArray(); 24 boolean isEmpty = jsonObject.isEmpty(); 25 boolean isNullObject = jsonObject.isNullObject(); 26 // 打印:4 27 System.out.println("是否数组:" + isArray + " 是否空:" + isEmpty + " 是否空对象:" 28 + isNullObject); 29 30 // 把JSONArray增加到JSONObject中 31 JSONArray jsonArray = new JSONArray(); 32 jsonArray.add(0, "lwc"); 33 jsonArray.add(1, "nxj"); 34 // 开始增加 35 jsonObject.element("student", jsonArray); 36 // 打印:5 37 System.out.println(jsonObject); 38 } 39 } 40 /* 41 打印结果 42 {"username":"lwc","password":"123"} 43 {"username":"lwc","password":"123","sex":"男"} 44 男 45 是否为数组:false 是否为空:false 是否为空对象:false 46 {"username":"lwc","password":"123","sex":"男","student":["lwc","nxj"]} 47 */
JSONArray
1 package com.itlwc.test; 2 3 import net.sf.json.JSONArray; 4 import net.sf.json.JSONObject; 5 6 public class Test { 7 public static void main(String[] args) { 8 //创建JSONArray 9 JSONArray jsonArray = new JSONArray(); 10 jsonArray.add(0, "lwc"); 11 jsonArray.add(1, "nxj"); 12 jsonArray.element("mxj"); 13 //打印:1 14 System.out.println(jsonArray); 15 16 //根据下标返回,打印:2 17 System.out.println(jsonArray.get(0)); 18 19 //根据下标设置新值,打印:3 20 jsonArray.set(0, "itlwc"); 21 System.out.println(jsonArray); 22 23 //把JSONObject放入到JSONArray中 24 JSONObject jsonObject = new JSONObject(); 25 jsonObject.put("username", "lwc"); 26 jsonObject.put("password", "123"); 27 //开始增加,打印:4 28 jsonArray.add(jsonObject); 29 System.out.println(jsonArray); 30 31 //遍历,打印:5 32 for(int i=0;i<jsonArray.size();i++){ 33 System.out.print(jsonArray.get(i)+"\t"); 34 } 35 } 36 } 37 /* 38 打印结果 39 ["lwc","nxj","mxj"] 40 lwc 41 ["itlwc","nxj","mxj"] 42 ["itlwc","nxj","mxj",{"username":"lwc","password":"123"}] 43 itlwc nxj mxj {"username":"lwc","password":"123"} 44 */
JavaBean与json字符串互转
1 package com.itlwc.test; 2 3 import net.sf.json.JSONObject; 4 5 import com.itlwc.entity.Student; 6 7 public class Test { 8 public static void main(String[] args) { 9 // JavaBean对象转json字符串 10 Student stu1 = new Student("lwc", "111111"); 11 JSONObject jsonObject = JSONObject.fromObject(stu1); 12 System.out.println(jsonObject); 13 14 // json字符串转JavaBean 15 String jsondata = "{\"password\":\"111111\",\"username\":\"lwc\"}"; 16 JSONObject jsonObject1 = JSONObject.fromObject(jsondata); 17 Student stu2 = (Student) JSONObject.toBean(jsonObject1, Student.class); 18 System.out.println(stu2); 19 } 20 } 21 /* 22 打印结果: 23 {"password":"111111","username":"lwc"} 24 用户: lwc 密码:111111 25 */
List与json字符串互转
1 package com.itlwc.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import net.sf.json.JSONArray; 7 import net.sf.json.JSONObject; 8 9 import com.itlwc.entity.Student; 10 11 public class Test { 12 public static void main(String[] args) { 13 // List转json字符串 14 List list = new ArrayList(); 15 list.add(new Student("lwc", "111111")); 16 list.add(new Student("nxj", "222222")); 17 JSONArray jsonArray = JSONArray.fromObject(list); 18 System.out.println(jsonArray); 19 20 // json字符串转List 21 List list1 = new ArrayList(); 22 String jsondata = "[{\"password\":\"111111\",\"username\":\"lwc\"},{\"password\":\"222222\",\"username\":\"nxj\"}]"; 23 JSONArray jsonArray1 = JSONArray.fromObject(jsondata); 24 for (int i = 0; i < jsonArray1.size(); i++) { 25 JSONObject jsonObject2 = jsonArray1.getJSONObject(i); 26 Student stu2 = (Student) JSONObject.toBean(jsonObject2, 27 Student.class); 28 list1.add(stu2); 29 } 30 System.out.println(list1); 31 } 32 } 33 /* 34 打印结果: 35 [{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}] 36 [用户: lwc 密码:111111, 用户: nxj 密码:222222] 37 */
Map与json字符串互转
1 package com.itlwc.test; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 import java.util.Set; 7 8 import net.sf.json.JSONObject; 9 10 import com.itlwc.entity.Student; 11 12 public class Test { 13 public static void main(String[] args) { 14 // Map转json字符串 15 Map map = new HashMap(); 16 map.put("1", new Student("lwc", "111111")); 17 map.put("2", new Student("nxj", "222222")); 18 JSONObject jsonMap = JSONObject.fromObject(map); 19 System.out.println(jsonMap); 20 21 // json字符串转Map 22 String jsondata = "{\"2\":{\"password\":\"222222\",\"username\":\"nxj\"},\"1\":{\"password\":\"111111\",\"username\":\"lwc\"}}"; 23 Map map1 = (Map) JSONObject.fromObject(jsondata); 24 Set set = map1.keySet(); 25 Iterator ite = set.iterator(); 26 while (ite.hasNext()) { 27 String key = (String) ite.next(); 28 JSONObject jsonObject = JSONObject.fromObject(map1.get(key)); 29 Student stu = (Student) JSONObject 30 .toBean(jsonObject, Student.class); 31 System.out.println(key + " " + stu); 32 } 33 34 } 35 } 36 /* 37 打印结果: 38 {"2":{"password":"222222","username":"nxj"},"1":{"password":"111111","username":"lwc"}} 39 2 用户: nxj 密码:222222 40 1 用户: lwc 密码:111111 41 */
JSONArray与List互转
1 package com.itlwc.test; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import net.sf.json.JSONArray; 8 import net.sf.json.JsonConfig; 9 10 import com.itlwc.entity.Student; 11 12 public class Test { 13 public static void main(String[] args) { 14 //List转型JSONArray 15 List<Student> list = new ArrayList<Student>(); 16 list.add(new Student("lwc", "111111")); 17 list.add(new Student("nxj", "222222")); 18 JSONArray jsonArray = JSONArray.fromObject(list); 19 System.out.println(jsonArray.toString()); 20 21 //JSONArray转型List 22 List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig()); 23 Iterator<Student> ite = list2.iterator(); 24 while(ite.hasNext()){ 25 Student stu =ite.next(); 26 System.out.println(stu); 27 } 28 } 29 } 30 /* 31 打印结果 32 [{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}] 33 用户: lwc 密码:111111 34 用户: nxj 密码:222222 35 */
JSONArray与数组互转
1 package com.itlwc.test; 2 3 import net.sf.json.JSONArray; 4 5 public class Test { 6 public static void main(String[] args) { 7 // Java数组转JSONArray 8 boolean[] boolArray = new boolean[] { true, false, true }; 9 JSONArray jsonArray = JSONArray.fromObject(boolArray); 10 System.out.println(jsonArray.toString()); 11 12 // JSONArray转Java数组 13 Object obj[] = jsonArray.toArray(); 14 for (Object o : obj) { 15 System.out.print(o + " "); 16 } 17 } 18 } 19 /* 20 打印结果 : 21 [true,false,true] 22 true false true 23 */
XML与JSON互转
需要导入xom-1.1.jar
1 package com.itlwc.test; 2 3 import net.sf.json.JSON; 4 import net.sf.json.JSONObject; 5 import net.sf.json.xml.XMLSerializer; 6 7 public class Test { 8 public static void main(String[] args) throws Exception { 9 // XML转JSON 10 String xml = "<root>" + "<name type='type'>zhaipuhong</name>" 11 + "<gender>male</gender>" + "<birthday>" + "<year>1970</year>" 12 + "<month>12</month>" + "<day>17</day>" + "</birthday>" 13 + "</root>"; 14 XMLSerializer xmlSerializer = new XMLSerializer(); 15 JSON json = xmlSerializer.read(xml); 16 System.out.println(json.toString(2)); 17 18 // JSON转XML 19 String jsondata = "{\"root\":{" + "\"name\":\"zhaipuhong\"," 20 + "\"gender\":\"male\"," + "\"birthday\":{" 21 + "\"year\":\"1970\"," + "\"month\":\"12\"," + "\"day\":\"17\"" 22 + "}" + "}" + "}"; 23 JSONObject jsonObject = JSONObject.fromObject(jsondata); 24 String xmlstr = new XMLSerializer().write(jsonObject); 25 System.out.println(xmlstr); 26 } 27 } 28 /* 29 打印结果: 30 { 31 "name": "zhaipuhong", 32 "gender": "male", 33 "birthday": { 34 "year": "1970", 35 "month": "12", 36 "day": "17" 37 } 38 } 39 <?xml version="1.0" encoding="UTF-8"?> 40 <o> 41 <root class="object"> 42 <birthday class="object"> 43 <day type="string">17</day> 44 <month type="string">12</month> 45 <year type="string">1970</year> 46 </birthday> 47 <gender type="string">male</gender> 48 <name type="string">zhaipuhong</name> 49 </root> 50 </o> 51 */