常用json与javabean互转的方法实现
程序员文章站
2022-06-25 18:29:34
jsonobject 与 jsonarray区别jsonobject:{"area": "武汉","name": "张三","age": 25}jsonarray:[{“area”: “武汉”,“na...
jsonobject 与 jsonarray区别
jsonobject:
{ "area": "武汉", "name": "张三", "age": 25 }
jsonarray:
[{ “area”: “武汉”, “name”: “张三”, “age”: 25 }, { “area”: “深圳”, “name”: “李四”, “age”: 22 }]
通俗来讲 jsonobject 是对象的json形式 jsonarry 是对象集合的json形式。
json 与javabean互转
json用阿里的fastjson 包
用例java对象
public class user { protected long id; protected string account; protected string password; protected string name; protected boolean gender; protected string telephone; @override public string tostring() { return "user{" + "id=" + id + ", account='" + account + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + ", gender=" + gender + ", telephone='" + telephone + '\'' + '}'; } public boolean isgender() { return gender; } public void setgender(boolean gender) { this.gender = gender; } public string gettelephone() { return telephone; } public void settelephone(string telephone) { this.telephone = telephone; } public string getname() { return name; } public void setname(string name) { this.name = name; } public long getid() { return id; } public void setid(long id) { this.id = id; } public string getaccount() { return account; } public void setaccount(string account) { this.account = account; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } }
1、javabean转json
方法一:通过java对象转成string再转成jsonobject
package com.handoop.gms.utils; import com.alibaba.fastjson.jsonobject; import com.handoop.gms.domain.user; public class testmain { public static void main(string []args){ //先通过构造函数初始化一个对象 user user=new user((long) 1,"admin","admin","张三",true,"123456"); //先将java对象转为string类型 string jsonstring= jsonobject.tojsonstring(user); //再将string类型转为jsonobject jsonobject jsonobject=jsonobject.parseobject(jsonstring); system.out.println(jsonobject); //转为jsonobject后就可以随时根据键值获取他的元素了 system.out.println(jsonobject.get("password")); } }
运行结果
方法2:java对象直接转json
package com.handoop.gms.utils; import com.alibaba.fastjson.jsonarray; import com.alibaba.fastjson.jsonobject; import com.handoop.gms.domain.user; public class testmain { public static void main(string []args){ //先通过构造函数初始化一个对象 user user=new user((long) 1,"admin","admin","张三",true,"123456"); jsonobject jsonobject= (jsonobject) jsonobject.tojson(user); system.out.println(jsonobject); } }
运行结果
json字符串转jsonobeject
public class testmain { public static void main(string []args){ string str="{\"password\":\"admin\",\"gender\":true,\"name\":\"张三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}"; jsonobject jsonobject=jsonobject.parseobject(str); system.out.println("account: "+jsonobject.get("account")+"---"+"paasword: "+jsonobject.get("password")); } }
运行结果
3.jsonstring 转jsonarray
public class testmain { public static void main(string []args){ string str="{\"data\":[{\"password\":\"admin\",\"gender\":true,\"name\":\"张三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}]}"; //先转成jsonobject jsonobject jsonobject=jsonobject.parseobject(str); //再将jsonobject中数组类型数据取出转成jsonarray jsonarray jsonarray=jsonobject.getjsonarray("data"); system.out.println(jsonarray.get(0)); } }
运行结果
4.json字符串转java对象
string str="{\"password\":\"admin\",\"gender\":true,\"name\":\"张三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}"; // 前面是json字符串 后面是java对象类型 user user=jsonobject.parseobject(str,user.class); system.out.println("account: "+user.getaccount()+"---"+"paasword: "+user.getpassword());
输出结果
到此这篇关于常用json与javabean互转的方法实现的文章就介绍到这了,更多相关json与javabean互转内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!