百度站内搜索代码,如何开发自己的app,湖南省疾控中心,四川建设网官文末会有读者福利简介:在程序开发过程中#xff0c;在参数传递#xff0c;函数返回值等方面#xff0c;越来越多的使用JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式#xff0c;同时也易于机器解析和生成、易于理解、阅读和撰写#xff0c;而且Json采… 文末会有读者福利简介:在程序开发过程中在参数传递函数返回值等方面越来越多的使用JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式同时也易于机器解析和生成、易于理解、阅读和撰写而且Json采用完全独立于语言的文本格式这使得Json成为理想的数据交换语言。JSON建构于两种结构“名称/值”对的集合(A Collection of name/value pairs)在不同的语言中它被理解为对象(Object), 记录(record), 结构(struct), 字典(dictionary), 有趣列表(keyed list), 哈希表(hash table)或者关联数组(associative array)。JSONObject依赖最后一行需要保留有两个jdk版本的实现json-lib-2.1-jdk13.jar和json-lib-2.1-jdk15.jarnet.sf.json-lib json-lib 2.4jdk15使用net.sf.json需要导入的jar包jar包下载https://pan.baidu.com/s/1iZiXw55TPwIxYFQQCaR9GwJSONObject创建JSONObject添加属性//创建JSONObjectJSONObject json new JSONObject();//添加属性json.put(username, 张三);json.put(password, 123);//打印System.out.println(json); //增加属性json.element(sex, 男);json.put(age, 18);System.out.println(json);根据key返回输出System.out.println(json.get(sex));判断输出对象的类型boolean isArray json.isArray();boolean isEmpty json.isEmpty();boolean isNullObject json.isNullObject();System.out.println(是否数组isArray, 是否空isEmpty, 是否空为空对象isNullObject);把JSONArray添加到JSONObject中/把JSONArray添加到JSONObject中JSONArray jsonArray new JSONArray();jsonArray.add(0, 张三);jsonArray.add(1, 123);//开始添加json.element(student, jsonArray);System.out.println(json);全部代码import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json {public static void main(String[] args) {//创建JSONObjectJSONObject json new JSONObject();//添加属性json.put(username, 张三);json.put(password, 123);//打印System.out.println(json);//增加属性json.element(sex, 男);json.put(age, 18);System.out.println(json);//根据key返回System.out.println(json.get(sex));//判断输出对象的类型boolean isArray json.isArray();boolean isEmpty json.isEmpty();boolean isNullObject json.isNullObject();System.out.println(是否数组isArray, 是否空isEmpty, 是否空为空对象isNullObject);System.out.println();//把JSONArray添加到JSONObject中JSONArray jsonArray new JSONArray();jsonArray.add(0, 张三);jsonArray.add(1, 123);//开始添加json.element(student, jsonArray);System.out.println(json);}}运行结果JSONArray创建JSONArray添加属性值//创建JSONArrayJSONArray jsonArray new JSONArray();//添加jsonArray.add(0, 张三);jsonArray.add(1, 123);jsonArray.element(男);System.根据下标返回输出System.out.println(jsonArray.get(0));根据下标设置新值修改jsonArray.set(0, 李四);System.out.println(jsonArray);把JSONObject放入到JSONArray中//把JSONObject放入到JSONArray中JSONObject jsonObject new JSONObject();jsonObject.put(username, 张三);jsonObject.put(password, 123);jsonArray.add(jsonObject);System.全部代码import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json {public static void main(String[] args) {//创建JSONArrayJSONArray jsonArray new JSONArray();//添加jsonArray.add(0, 张三);jsonArray.add(1, 123);jsonArray.element(男);System.out.println(jsonArray);//根据下标返回输出System.out.println(jsonArray.get(0));//根据下标设置新值修改jsonArray.set(0, 李四);System.out.println(jsonArray);//把JSONObject放入到JSONArray中JSONObject jsonObject new JSONObject();jsonObject.put(username, 张三);jsonObject.put(password, 123);jsonArray.add(jsonObject);System.out.println(jsonArray);//循环输出for(int i 0; i jsonArray.size(); i) {System.out.println(jsonArray.get(i));}}}运行结果JavaBean与json字符串互转student类public class Student {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public Student(String username, String password) {super();this.username username;this.password password;}public Student() {super();// TODO Auto-generated constructor stub}Overridepublic String toString() {return Student [username username , password password ];}}定义对象JavaBean对象转json字符串//定义对象Student stu new Student(张三, 123456);//JavaBean对象转json字符串JSONObject jsonObject JSONObject.fromObject(stu);System.out.println(jsonObject);json字符串转为javaBean//json字符串转为javaBean//定义json字符串String jsondata {username:李四, password:123};//转为json对象JSONObject json JSONObject.fromObject(jsondata);//转为JavaBean对象Student stu2 (Student)JSONObject.toBean(json, Student.class);System.out.println(stu2.toString());全部代码import net.sf.json.JSONObject; public class Json {public static void main(String[] args) {//定义对象Student stu new Student(张三, 123456);//JavaBean对象转json字符串JSONObject jsonObject JSONObject.fromObject(stu);System.out.println(jsonObject);//json字符串转为javaBean//定义json字符串String jsondata {username:李四, password:123};//转为json对象JSONObject json JSONObject.fromObject(jsondata);//转为JavaBean对象Student stu2 (Student)JSONObject.toBean(json, Student.class);System.out.println(stu2.toString());}}输出结果List与json字符串互转先定义list集合list转json字符串//定义list集合List list new ArrayList();list.add(new Student(张三, 123));list.add(new Student(李四, 456));//list转json字符串JSONArray jsonArray JSONArray.fromObject(list);System.out.println(jsonArray);json字符串转list//json字符串转listList list2 new ArrayList();String jsondata [{password:123,username:张三},{password:456,username:李四}];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);list2.add(stu2);}System.out.println(list2);全部代码import java.util.ArrayList;import java.util.List; import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json {public static void main(String[] args) {//定义list集合List list new ArrayList();list.add(new Student(张三, 123));list.add(new Student(李四, 456));//list转json字符串JSONArray jsonArray JSONArray.fromObject(list);System.out.println(jsonArray);//json字符串转listList list2 new ArrayList();String jsondata [{password:123,username:张三},{password:456,username:李四}];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);list2.add(stu2);}System.out.println(list2);}}运行结果Map与json字符串互转定义map集合Map转json字符串//定义map集合Map map new HashMap();map.put(1, new Student(张三, 123));map.put(2, new Student(李四, 456));//Map转json字符串JSONObject jsonMap JSONObject.fromObject(map);System.out.println(jsonMap);定义字符串map集合map集合字符串转为map//定义字符串map集合String jsondata {1:{password:123,username:张三},2:{password:456,username:李四}};//map集合字符串转为mapMap map2 (Map)JSONObject.fromObject(jsondata);Set set map2.keySet();//定义迭代器迭代输出Iterator ite set.iterator();while(ite.hasNext()) {//取出一个字符串对象String key (String)ite.next();//转为json格式JSONObject jsonObject JSONObject.fromObject(map2.get(key));//转为对象Student stu (Student)JSONObject.toBean(jsonObject, Student.class);System.out.println(key stu);}全部代码import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set; import net.sf.json.JSONObject; public class Json {public static void main(String[] args) {//定义map集合Map map new HashMap();map.put(1, new Student(张三, 123));map.put(2, new Student(李四, 456));//Map转json字符串JSONObject jsonMap JSONObject.fromObject(map);System.out.println(jsonMap);//定义字符串map集合String jsondata {1:{password:123,username:张三},2:{password:456,username:李四}};//map集合字符串转为mapMap map2 (Map)JSONObject.fromObject(jsondata);Set set map2.keySet();//定义迭代器迭代输出Iterator ite set.iterator();while(ite.hasNext()) {//取出一个字符串对象String key (String)ite.next();//转为json格式JSONObject jsonObject JSONObject.fromObject(map2.get(key));//转为对象Student stu (Student)JSONObject.toBean(jsonObject, Student.class);System.out.println(key stu);}}}运行结果:SONArray与List互转定义list集合List转型JSONArray//定义list集合List list new ArrayList();list.add(new Student(张三, 123));list.add(new Student(李四, 456));//List转型JSONArrayJSONArray jsonArray JSONArray.fromObject(list);System.out.println(jsonArray.toString());JSONArray转型ListJSONArray是用的上面的那个jsonArray变量//JSONArray转型ListList list2 JSONArray.toList(jsonArray, new Student(), new JsonConfig());Iterator ite list2.iterator();while(ite.hasNext()) {Student stu ite.next();System.out.println(stu);}全部代码import java.util.ArrayList;import java.util.Iterator;import java.util.List; import net.sf.json.JSONArray;import net.sf.json.JsonConfig; public class Json {public static void main(String[] args) {//定义list集合List list new ArrayList();list.add(new Student(张三, 123));list.add(new Student(李四, 456));//List转型JSONArrayJSONArray jsonArray JSONArray.fromObject(list);System.out.println(jsonArray.toString());//JSONArray转型ListList list2 JSONArray.toList(jsonArray, new Student(), new JsonConfig());Iterator ite list2.iterator();while(ite.hasNext()) {Student stu ite.next();System.out.println(stu);}}}运行结果JSONArray与数组互转定义数组数组转JSONArray//定义数组boolean[] boolArray {true, false, true};//java数组转JSONArrayJSONArray jsonArray JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());JSONArray转java数组//JSONArray转java数组Object obj[] jsonArray.toArray();for(Object o : obj) {System.out.print(o);}全部代码import net.sf.json.JSONArray; public class Json {public static void main(String[] args) {//定义数组boolean[] boolArray {true, false, true};//java数组转JSONArrayJSONArray jsonArray JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());//JSONArray转java数组Object obj[] jsonArray.toArray();for(Object o : obj) {System.out.print(o);}}}运行结果感谢阅读 喜欢的朋友和爱学习的小伙伴可关注下笔者 会定期发布优质文章的哦以下是笔者整理的大量的java面试和结构资料有需要的朋友需‘关注’‘私信’【资料】即可获得本文链接https://blog.csdn.net/qq_40205116/article/details/102921564