在程序开发过程中,在参数传递,函数返回值等方面,越来越多的使用json。json(javascript object notation)是一种轻量级的数据交换格式,同时也易于机器解析和生成、易于理解、阅读和撰写,而且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.jar
<dependency> <groupid>net.sf.json-lib</groupid> <artifactid>json-lib</artifactid> <version>2.4</version> <classifier>jdk15</classifier></dependency>
使用net.sf.json需要导入的jar包
jar包下载:
https://pan.baidu.com/s/1izixw55tpwixyfqqcar9gw
创建jsonobject,添加属性
//创建jsonobjectjsonobject json = new jsonobject();//添加属性json.put("urname", "张三");json.put("password", "123");//打印system.out.println(json); //增加属性json.element("x", "男");json.put("age", 18);system.out.println(json);
根据key返回输出
system.out.println(json.get("x"));
判断输出对象的类型
boolean isarray = json.isarray();boolean impty = json.impty();boolean isnullobject = json.isnullobject();system.out.println("是否数组:"+isarray+", 是否空:"+impty+", 是否空为空对象:"+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("urname", "张三");json.put("password", "123");//打印system.out.println(json);//增加属性json.element("x", "男");json.put("age", 18);system.out.println(json);//根据key返回system.out.println(json.get("x"));//判断输出对象的类型boolean isarray = json.isarray();boolean impty = json.impty();boolean isnullobject = json.isnullobject();system.out.println("是否数组:"+isarray+", 是否空:"+impty+", 是否空为空对象:"+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,添加属性值
//创建jsonarrayjsonarray jsonarray = new jsonarray();//添加jsonarray.add(0, "张三");jsonarray.add(1, "123");jsonarray.element("男");system.
根据下标返回输出
system.out.println(jsonarray.get(0));
根据下标设置新值,修改
jsonarray.t(0, "李四");system.out.println(jsonarray);把jsonobject放入到jsonarray中//把jsonobject放入到jsonarray中jsonobject jsonobject = new jsonobject();jsonobject.put("urname", "张三");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.t(0, "李四");system.out.println(jsonarray);//把jsonobject放入到jsonarray中jsonobject jsonobject = new jsonobject();jsonobject.put("urname", "张三");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));}}}
运行结果
student类:
public class student {private string urname;private string password;public string geturname() {return urname;}public void turname(string urname) {this.urname = urname;}public string getpassword() {return password;}public void tpassword(string password) {this.password = password;}public student(string urname, string password) {super();this.urname = urname;this.password = password;}public student() {super();// todo auto-generated constructor stub}@overridepublic string tostring() {return "student [urname=" + urname + ", 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 = "{"urname":"李四", "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 = "{"urname":"李四", "password":"123"}";//转为json对象jsonobject json = jsonobject.fromobject(jsondata);//转为javabean对象student stu2 = (student)jsonobject.tobean(json, student.class);system.out.println(stu2.tostring());}}
输出结果:
先定义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","urname":"张三"},{"password":"456","urname":"李四"}]";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","urname":"张三"},{"password":"456","urname":"李四"}]";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集合,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","urname":"张三"},"2":{"password":"456","urname":"李四"}}";//map集合字符串转为mapmap map2 = (map)jsonobject.fromobject(jsondata);t t = map2.keyt();//定义迭代器,迭代输出iterator ite = t.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.t; 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","urname":"张三"},"2":{"passwor刻舟求剑古文翻译d":"456","urname":"李四"}}";//map集合字符串转为mapmap map2 = (map)jsonobject.fromobject(jsondata);t t = map2.keyt();//定义迭代器,迭代输出iterator ite = t.iterator();while(ite.hasnext()) {//取出一个字符串对象string key = (string)ite.next();//转为j生母韵母son格式jsonobject jsonobject = jsonobject.fromobject(map2.get(key));//转为对象student stu = (student)jsonobject.tobean(jsonobject, student.class);system.out.println(key+" "+stu);}}}
运行结果:
定义list集合,list转型jsonarray
//定义list集合list<student> list = new arraylist<student>();list.add(new student("张三", "123"));list.add(new student("李四", "456"));//list转型jsonarrayjsonarray jsonarray = jsonarray.fromobject(list);system.out.println(jsonarray.tostring());
jsonarray转型list,jsonarray是用的上面的那个jsonarray变量
//jsonarray转型listlist<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);}
全部代码
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<student> list = new arraylist<student>();list.add(new student("张三", "123"));list.add(new student("李四", "456"));//list转型jsonarrayjsonarray jsonarray = jsonarray.fromobject(list);system.out.println(jsonarray.tostring());//jsonarray转型listlist<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);}}}
运行结果
定义数组,数组转jsonarray
//定义数组boolean[] boolarray = {true, fal, 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+"t");}
全部代码
import net.sf.json.jsonarray; public class json {public static void main(string[] args) {//定义数组boolean[] boolarray = {true, fal, true};//java数组转jsonarrayjsonarray jsonarray = jsonarray.fromobject(boolarray);system.out.println(jsonarray.tostring());//jsonarr青春痘该怎么治ay转java数组object obj[] = jsonarray.toarray();for(object o : obj) {system.out.print(o+"t");}}}
本文发布于:2023-04-05 03:24:39,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/05dc25831984952574b9243a021a6d49.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:jsonobject判断key是否存在某个key(java的foreach用法).doc
本文 PDF 下载地址:jsonobject判断key是否存在某个key(java的foreach用法).pdf
留言与评论(共有 0 条评论) |