转载 解析json之net.sf.json https://blog.csdn.net/itlwc/article/details/38442667
一、介绍
使用之前需要导入的jar包:
json-lib-2.4-jdk15.jar commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar <!--这个包我没导入,也可以使用-->
二、使用
1.JSONObject的使用
public class JSONObjectUse { public static void main(String[] args) { //创建JSONObject对象 JSONObject jsonObject = new JSONObject(); jsonObject.put("username","wln"); jsonObject.put("password","123"); System.out.println("1:" + jsonObject); //增加属性 jsonObject.element("sex","男"); System.out.println("2:" + jsonObject); //判断输出对象的类型 boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("3:" + "是否是数组:" + isArray +" 是否是空:" + isEmpty + " 是否是空对象:" + isNullObject); //创建JSONArray JSONArray jsonArray = new JSONArray(); jsonArray.add(0,"aa"); jsonArray.add("BB"); jsonArray.add(1,"AB"); jsonArray.add("cc"); //将JSONArray 添加到JSONObject jsonObject.element("student",jsonArray); System.out.println("4:" + jsonObject); } } 结果: 1:{"username":"wln","password":"123"} 2:{"username":"wln","password":"123","sex":"男"} 3:是否是数组:false 是否是空:false 是否是空对象:false 4:{"username":"wln","password":"123","sex":"男","student":["aa","AB","BB","cc"]}
2.JSONArray的使用
public class JSONArrayUse { public static void main(String[] args) { //创建JSONArray对象 JSONArray jsonArray = new JSONArray(); jsonArray.add(0,"aa"); jsonArray.add(1,"BB"); jsonArray.element("cc"); jsonArray.add("DD"); System.out.println("1:" + jsonArray); //根据下标获取数据 System.out.println("2:" + jsonArray.get(0)); //根据下标设置数据 jsonArray.set(0,"AAA"); System.out.println("3:" + jsonArray); //创建JSONObject JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "lwc"); jsonObject.put("password", "123"); //把JSONObject放入到JSONArray中 jsonArray.add(jsonObject); System.out.println("4:" + jsonArray); //遍历 System.out.println("5:"); for(int i=0;i<jsonArray.size();i++){ System.out.print(jsonArray.get(i)+" "); } } } 结果: 1:["aa","BB","cc","DD"] 2:aa 3:["AAA","BB","cc","DD"] 4:["AAA","BB","cc","DD",{"username":"lwc","password":"123"}] 5: AAA BB cc DD {"username":"lwc","password":"123"}
3.JavaBean与JSON字符串之间的转换
public class JavaBeanUse { public static void main(String[] args) { //将JavaBean转换为JSONObject Student student = new Student("wln", "22"); JSONObject jsonObject = JSONObject.fromObject(student); System.out.println("1:" + jsonObject); //将JSONString转换为JSONObject String jsonStr = "{"name":"nana","age":"33"}"; JSONObject jsonObject1 = JSONObject.fromObject(jsonStr); Student stu = (Student) JSONObject.toBean(jsonObject1,Student.class); System.out.println("2:" + stu); System.out.println("3:" + stu.getName() +" "+ stu.getAge()); } } 结果: 1:{"age":"22","name":"wln"} 2:com.springboot.devtools.jsonUse.entity.Student@61a485d2 3:nana 33
public class Student { private String name; private String age; public Student() { } public Student(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
4.List与JSON字符串之间的转换
public class ListUse { public static void main(String[] args) { //将List转成JSONArray List list = new ArrayList(); list.add(new Student("wln","22")); list.add(new Student("nana","33")); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println("1:" + jsonArray); //将JSONString转成List List list1 = new ArrayList(); String strJSON = "[{"name":"dada","age":"44"},{"name":"xiaoxiao","age":"55"}]"; JSONArray jsonArray1 = JSONArray.fromObject(strJSON); for(int i = 0;i < jsonArray1.size(); i++) { JSONObject jsonObject = jsonArray1.getJSONObject(i); Student stu = (Student) JSONObject.toBean(jsonObject,Student.class); list1.add(stu); } System.out.println("2:" + list1); } } 结果: 1:[{"age":"22","name":"wln"},{"age":"33","name":"nana"}] 2:[com.springboot.devtools.jsonUse.entity.Student@7946e1f4, com.springboot.devtools.jsonUse.entity.Student@3c09711b]
5.Map与JSON字符串之间的转换
public class MapUse { public static void main(String[] args) { //Map转JSONString Map map = new HashMap(); map.put("1", new Student("wln","22")); map.put("2", new Student("nana","33")); JSONObject jsonMap = JSONObject.fromObject(map); System.out.println("1:" + map); //JSONString转Map String jsonStr = "{"1":{"name":"dada","age":"44"},"2":{"name":"xiaoxiao","age":"55"}}"; Map map1 = (Map) JSONObject.fromObject(jsonStr); Set set = map1.keySet(); Iterator ite = set.iterator(); while (ite.hasNext()) { String key = (String) ite.next(); JSONObject jsonObject = JSONObject.fromObject(map1.get(key)); Student stu = (Student) JSONObject.toBean(jsonObject, Student.class); System.out.println("2:" + key + " " + stu); } } } 结果: 1:{1=com.springboot.devtools.jsonUse.entity.Student@1b40d5f0, 2=com.springboot.devtools.jsonUse.entity.Student@ea4a92b} 2:1 com.springboot.devtools.jsonUse.entity.Student@3c09711b 2:2 com.springboot.devtools.jsonUse.entity.Student@5cc7c2a6
6.JSONArray与List之间的转换
public class JSONArrayToList { public static void main(String[] args) { //List转换为JSONArray List<Student> list = new ArrayList<Student>(); list.add(new Student("wln","22")); list.add(new Student("nana","33")); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println("1:" + jsonArray); //JSONArray转换为List List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig()); Iterator<Student> ite = list2.iterator(); while (ite.hasNext()) { Student stu = ite.next(); System.out.println("2:" + stu); } } } 结果: 1:[{"age":"22","name":"wln"},{"age":"33","name":"nana"}] 2:com.springboot.devtools.jsonUse.entity.Student@69d9c55 2:com.springboot.devtools.jsonUse.entity.Student@13a57a3b
7.JSONArray与数组之间的转换
public class JSONArrayToArray { public static void main(String[] args) { //Java数组转换JSONArray boolean[] boolArray = new boolean[] {true, false, true}; JSONArray jsonArray = JSONArray.fromObject(boolArray); System.out.println("1:"+ jsonArray.toString()); //JSONArray转换Java数组 Object obj[] = jsonArray.toArray(); for (Object o : obj) { System.out.print("2:" + o + " "); } } } 结果: 1:[true,false,true] 2:true 2:false 2:true
8.XML与JSON之间的转换
需要导入xom-1.1.jar
public class XMLToJSON { public static void main(String[] args) throws Exception { //XML转换JSON String xml = "<root>" + "<name type='type'>wln</name>" + "<gender>woman</gender>" + "<birthday>" + "<year>1970</year>" + "<month>12</month>" + "<day>17</day>" + "</birthday>" + "</root>"; XMLSerializer xmlSerializer = new XMLSerializer(); JSON json = xmlSerializer.read(xml); System.out.println("1:" + json.toString(2)); //JSON转换XML String jsonStr = "{"root":{" + ""name":"wln"," + ""gender":"woman"," + ""birthday":{" + ""year":"1970"," + ""month":"12"," + ""day":"17"" + "}" + "}" + "}"; JSONObject jsonObject = JSONObject.fromObject(jsonStr); String xmlStr = new XMLSerializer().write(jsonObject); System.out.println("2:" + xmlStr); } } 结果: 1:{ "name": "wln", "gender": "woman", "birthday": { "year": "1970", "month": "12", "day": "17" } } 2:<?xml version="1.0" encoding="UTF-8"?> <o><root class="object"><birthday class="object"><day type="string">17</day><month type="string">12</month><year type="string">1970</year></birthday><gender type="string">woman</gender><name type="string">wln</name></root></o>