net.sf.json 骑猪看日落 2023-01-03 01:44 5阅读 0赞 ### 文章目录 ### * net.sf.json * * pom依赖 * JSONObject * JSONArray * JavaBean和JSON字符串互转 * List与JSON字符串互转 * Map与JSON字符串互转 * JSONArray与List互转 * JSONArray与数组互转 # net.sf.json # 在Java中较为常见的Json解析工具有阿里巴巴的fastjson,但由于项目中自带有net.sf.json包。因此使用该包进行Json的构建和解析。 ## pom依赖 ## <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> ## JSONObject ## import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test { public static void main(String[] args) { // 创建JSONObject JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "abc"); jsonObject.put("password", "123"); // 打印:1 System.out.println(jsonObject); //{"username":"abc","password":"123"} // 增加属性,打印:2 jsonObject.element("sex", "男"); System.out.println(jsonObject); //{"username":"abc","password":"123","sex":"男"} // 根据key返回,打印:3 System.out.println(jsonObject.get("sex")); //男 // 判读输出对象的类型 boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); // 打印:4 System.out.println("是否数组:" + isArray + " 是否空:" + isEmpty + " 是否空对象:" + isNullObject); //是否为数组:false 是否为空:false 是否为空对象:false // 把JSONArray增加到JSONObject中 JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "111"); jsonArray.add(1, "222"); // 开始增加 jsonObject.element("student", jsonArray); // 打印:5 System.out.println(jsonObject); //{"username":"abc","password":"123","sex":"男","student":["111","222"]} } } ## JSONArray ## import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test { public static void main(String[] args) { //创建JSONArray JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "111"); jsonArray.add(1, "222"); jsonArray.element("333"); //打印:1 System.out.println(jsonArray); //["111","222","333"] //根据下标返回,打印:2 System.out.println(jsonArray.get(0)); //111 //根据下标设置新值,打印:3 jsonArray.set(0, "it111"); System.out.println(jsonArray); //["it111","222","333"] //把JSONObject放入到JSONArray中 JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "lyh"); jsonObject.put("password", "123"); //开始增加,打印:4 jsonArray.add(jsonObject); System.out.println(jsonArray); //["it111","222","333",{"username":"lyh","password":"123"}] //遍历,打印:5 for(int i=0;i<jsonArray.size();i++){ System.out.print(jsonArray.get(i)+"\t"); } //it111 222 333 {"username":"lyh","password":"123"} } } ## JavaBean和JSON字符串互转 ## public class Test { public static void main(String[] args) { // JavaBean对象转json字符串 Student stu1 = new Student("lyh", "111111"); JSONObject jsonObject = JSONObject.fromObject(stu1); System.out.println(jsonObject); //{"password":"111111","username":"lyh"} // json字符串转JavaBean String jsondata = "{\"password\":\"111111\",\"username\":\"lyh\"}"; JSONObject jsonObject1 = JSONObject.fromObject(jsondata); Student stu2 = (Student) JSONObject.toBean(jsonObject1, Student.class); System.out.println(stu2); //用户: lyh 密码:111111 } } ## List与JSON字符串互转 ## public class Test { public static void main(String[] args) { // List转json字符串 List list = new ArrayList(); list.add(new Student("lyh", "111111")); list.add(new Student("java", "222222")); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray); //[{"password":"111111","username":"lyh"},{"password":"222222","username":"java"}] // json字符串转List List list1 = new ArrayList(); String jsondata = "[{\"password\":\"111111\",\"username\":\"lyh\"},{\"password\":\"222222\",\"username\":\"java\"}]"; JSONArray jsonArray1 = JSONArray.fromObject(jsondata); for (int i = 0; i < jsonArray1.size(); i++) { JSONObject jsonObject2 = jsonArray1.getJSONObject(i); Student stu2 = (Student) JSONObject.toBean(jsonObject2, Student.class); list1.add(stu2); } System.out.println(list1); //[用户: lyh 密码:111111, 用户: java 密码:222222] } } ## Map与JSON字符串互转 ## public class Test { public static void main(String[] args) { // Map转json字符串 Map map = new HashMap(); map.put("1", new Student("lyh", "111111")); map.put("2", new Student("java", "222222")); JSONObject jsonMap = JSONObject.fromObject(map); System.out.println(jsonMap); // {"2":{"password":"222222","username":"java"},"1":{"password":"111111","username":"lyh"}} // json字符串转Map String jsondata = "{\"2\":{\"password\":\"222222\",\"username\":\"java\"},\"1\":{\"password\":\"111111\",\"username\":\"lyh\"}}"; Map map1 = (Map) JSONObject.fromObject(jsondata); 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(key + " " + stu); //2 用户: java 密码:222222 //1 用户: lyh 密码:111111 } } } ## JSONArray与List互转 ## import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; import com.itlwc.entity.Student; public class Test { public static void main(String[] args) { //List转型JSONArray List<Student> list = new ArrayList<Student>(); list.add(new Student("lyh", "111111")); list.add(new Student("java", "222222")); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray.toString()); //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(stu); } } } /* 打印结果 [{"password":"111111","username":"lyh"},{"password":"222222","username":"java"}] 用户: lyh 密码:111111 用户: java 密码:222222 ## JSONArray与数组互转 ## import net.sf.json.JSONArray; public class Test { public static void main(String[] args) { // Java数组转JSONArray boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray = JSONArray.fromObject(boolArray); System.out.println(jsonArray.toString()); // JSONArray转Java数组 Object obj[] = jsonArray.toArray(); for (Object o : obj) { System.out.print(o + " "); } } } /* 打印结果 : [true,false,true] true false true
还没有评论,来说两句吧...